-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathpastebin.vala
More file actions
159 lines (124 loc) · 5.11 KB
/
pastebin.vala
File metadata and controls
159 lines (124 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
/***
BEGIN LICENSE
Copyright (C) 2011-2012 Giulio Collura <random.cpp@gmail.com>
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>
END LICENSE
***/
namespace Scratch.Services {
public class PasteBin : GLib.Object {
public const string NEVER = "N";
public const string TEN_MINUTES = "10M";
public const string HOUR = "1H";
public const string DAY = "1D";
public const string MONTH = "1M";
public const string PRIVATE = "1";
public const string PUBLIC = "0";
public static bool submit (out string link, string paste_code, string paste_name,
string paste_private, string paste_expire_date,
string paste_format) {
link = null;
if (paste_code.length == 0) { link = "No text to paste"; return false; }
string api_url = "https://pastebin.com/api/api_post.php";
var session = new Soup.Session ();
var message = new Soup.Message ("POST", api_url);
string request = Soup.Form.encode (
"api_option", "paste",
"api_dev_key", "67480801fa55fc0977f7561cf650a339",
"api_paste_code", paste_code,
"api_paste_name", paste_name,
"api_paste_private", paste_private,
"api_paste_expire_date", paste_expire_date,
"api_paste_format", paste_format);
message.set_request_body_from_bytes ("application/x-www-form-urlencoded", new Bytes (request.data));
message.set_flags (Soup.MessageFlags.NO_REDIRECT);
Bytes output;
try {
output = session.send_and_read (message);
} catch (Error e) {
return false;
}
var output_s = (string) output.get_data ();
link = output_s;
if (Uri.parse_scheme (output_s) == null || message.status_code != 200) {
// A URI was not returned
return false;
}
return true;
}
}
}
public class Scratch.Plugins.Pastebin : Peas.ExtensionBase, Scratch.Services.ActivatablePlugin {
GLib.MenuItem? menuitem = null;
GLib.Menu? share_menu = null;
public Object object { owned get; construct; }
Scratch.Services.Document? doc = null;
Scratch.Services.Interface plugins;
const string ACTION_GROUP = "pastebin";
const string ACTION_PREFIX = ACTION_GROUP + ".";
const string ACTION_SHOW = "action-show";
SimpleActionGroup actions;
const ActionEntry[] ACTION_ENTRIES = {
{ACTION_SHOW, show_paste_bin_upload_dialog }
};
public void update_state () {
}
public void activate () {
plugins = (Scratch.Services.Interface) object;
plugins.hook_document.connect ((doc) => {
this.doc = doc;
});
plugins.hook_share_menu.connect (on_hook_share_menu);
}
void on_hook_share_menu (GLib.MenuModel menu) {
if (menuitem != null) {
return;
}
add_actions (menu);
}
void add_actions (GLib.MenuModel menu) {
if (actions == null) {
actions = new SimpleActionGroup ();
actions.add_action_entries (ACTION_ENTRIES, this);
}
plugins.manager.window.insert_action_group (ACTION_GROUP, actions);
share_menu = (GLib.Menu) menu;
menuitem = new GLib.MenuItem (_("Upload to Pastebin"), ACTION_PREFIX + ACTION_SHOW);
share_menu.append_item (menuitem);
}
void remove_actions () {
int length = share_menu.get_n_items ();
for (var i = length - 1; i >= 0; i--) {
var action_name = share_menu.get_item_attribute_value (
i,
GLib.Menu.ATTRIBUTE_ACTION,
GLib.VariantType.STRING
).get_string ();
if (action_name.has_prefix (ACTION_PREFIX)) {
share_menu.remove (i);
}
}
plugins.manager.window.insert_action_group (ACTION_GROUP, null);
}
void show_paste_bin_upload_dialog () {
MainWindow window = plugins.manager.window;
new Dialogs.PasteBinDialog (window, doc);
}
public void deactivate () {
remove_actions ();
}
}
[ModuleInit]
public void peas_register_types (GLib.TypeModule module) {
var objmodule = module as Peas.ObjectModule;
objmodule.register_extension_type (typeof (Scratch.Services.ActivatablePlugin),
typeof (Scratch.Plugins.Pastebin));
}