Skip to content

Commit

Permalink
Don't try to be too clever with Pastebin API (elementary#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmhewitt authored and danirabbit committed Mar 15, 2018
1 parent a470f67 commit 330fd79
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 74 deletions.
57 changes: 10 additions & 47 deletions plugins/pastebin/pastebin.vala
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ using Soup;
namespace Scratch.Services {

public class PasteBin : GLib.Object {

public const int PASTE_ID_LEN = 8;

public const string NEVER = "N";
public const string TEN_MINUTES = "10M";
public const string HOUR = "1H";
Expand All @@ -36,21 +33,11 @@ namespace Scratch.Services {
public const string PUBLIC = "0";


public static int submit (out string link, string paste_code, string paste_name,
public static bool submit (out string link, string paste_code, string paste_name,
string paste_private, string paste_expire_date,
string paste_format) {

/* Code meaning:
0 = it's all ok
1 = generic error
2 = text (paste_code) is empty
3 = invalid file format
... maybe we should add and handle other errors...
*/

//check input values
if (paste_code.length == 0) {link=""; return 2; }

if (paste_code.length == 0) { link = "No text to paste"; return false; }

string api_url = "http://pastebin.com/api/api_post.php";

Expand All @@ -72,39 +59,15 @@ namespace Scratch.Services {
session.send_message (message);

var output = (string) message.response_body.data;

//check return value
if (output[0:6] != "ERROR:") {

//we need only pastebin url len + id len
output = output[0:20+PASTE_ID_LEN];
debug(output);

link = output;

} else {

//paste error

link = "";
switch(output) {
case "ERROR: Invalid POST request, or \"paste_code\" value empty":
return 2;

case "ERROR: Invalid file format":
return 3;

default:
return 1;

}

}

return 0;

link = output;

if (Uri.parse_scheme (output) == null) {
// A URI was not returned
return false;
}

return true;
}

}
}

Expand Down
31 changes: 4 additions & 27 deletions plugins/pastebin/pastebin_dialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -431,32 +431,12 @@ namespace Scratch.Dialogs {
var box = new Gtk.Box (Gtk.Orientation.VERTICAL, 10);
stack.add (box);

if (submit_result == 0) {
if (submit_result) {
//paste successfully
var link_button = new Gtk.LinkButton (link);
box.pack_start (link_button, false, true, 25);
} else {
//paste error
var error_desc = new StringBuilder();

switch(submit_result) {
case 2:
error_desc.append ("The text is void!");
break;

case 3:
error_desc.append ("The text format doesn't exist");
break;

default:
error_desc.append ("An error occured");
break;

}

error_desc.append ("\n" + "The text was sent");
var err_label = new Gtk.Label (error_desc.str);

var err_label = new Gtk.Label (link);
box.pack_start (err_label, false, true, 0);
}

Expand All @@ -465,18 +445,15 @@ namespace Scratch.Dialogs {
}


private int submit_paste (out string link) {
private bool submit_paste (out string link) {
// Get the values
string paste_code = this.doc.get_text ();
string paste_name = name_entry.text;
string paste_format = format_combo.get_active_id ();
string paste_private = private_check.get_active () == true ? PasteBin.PRIVATE : PasteBin.PUBLIC;
string paste_expire_date = expiry_combo.get_active_id ();

int submit_result = PasteBin.submit (out link, paste_code, paste_name, paste_private,
paste_expire_date, paste_format);

return submit_result;
return PasteBin.submit (out link, paste_code, paste_name, paste_private, paste_expire_date, paste_format);
}

private void populate_expiry_combo () {
Expand Down

0 comments on commit 330fd79

Please sign in to comment.