/
geniuspaste.c
797 lines (663 loc) · 24.6 KB
/
geniuspaste.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
/*
* geniuspaste - paste your code on your favorite pastebin.
*
* Copyright 2012 Enrico "Enrix835" Trotta <enrico.trt@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <libsoup/soup.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef HAVE_CONFIG_H
#include "config.h" /* for the gettext domain */
#endif
#include <geanyplugin.h>
#define PLUGIN_NAME "GeniusPaste"
#define PLUGIN_VERSION "0.3"
#ifdef G_OS_WIN32
#define USERNAME g_getenv("USERNAME")
#else
#define USERNAME g_getenv("USER")
#endif
/* stay compatible with GTK < 2.24 */
#if !GTK_CHECK_VERSION(2,24,0)
#define gtk_combo_box_text_new gtk_combo_box_new_text
#define gtk_combo_box_text_append_text gtk_combo_box_append_text
#define GTK_COMBO_BOX_TEXT GTK_COMBO_BOX
#endif
GeanyPlugin *geany_plugin;
GeanyData *geany_data;
GeanyFunctions *geany_functions;
static GtkWidget *main_menu_item = NULL;
typedef struct
{
gchar *name;
GKeyFile *config;
}
Pastebin;
GSList *pastebins = NULL;
static struct
{
GtkWidget *combo;
GtkWidget *check_button;
GtkWidget *author_entry;
} widgets;
static gchar *config_file = NULL;
static gchar *author_name = NULL;
static gchar *pastebin_selected = NULL;
static gboolean check_button_is_checked = FALSE;
PLUGIN_VERSION_CHECK(147)
PLUGIN_SET_TRANSLATABLE_INFO(LOCALEDIR, GETTEXT_PACKAGE, PLUGIN_NAME,
_("Paste your code on your favorite pastebin"),
PLUGIN_VERSION, "Enrico Trotta <enrico.trt@gmail.com>")
static void pastebin_free(Pastebin *pastebin)
{
g_key_file_unref(pastebin->config);
g_free(pastebin->name);
g_free(pastebin);
}
/* like g_key_file_has_group() but sets error if the group is missing */
static gboolean ensure_keyfile_has_group(GKeyFile *kf,
const gchar *group,
GError **error)
{
if (g_key_file_has_group(kf, group))
return TRUE;
else
{
g_set_error(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
_("Group \"%s\" not found."), group);
return FALSE;
}
}
/* like g_key_file_has_key() but sets error if either the group or the key is
* missing */
static gboolean ensure_keyfile_has_key(GKeyFile *kf,
const gchar *group,
const gchar *key,
GError **error)
{
if (! ensure_keyfile_has_group(kf, group, error))
return FALSE;
else if (g_key_file_has_key(kf, group, key, NULL))
return TRUE;
else
{
g_set_error(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
_("Group \"%s\" has no key \"%s\"."), group, key);
return FALSE;
}
}
static Pastebin *pastebin_new(const gchar *path,
GError **error)
{
Pastebin *pastebin = NULL;
GKeyFile *kf = g_key_file_new();
if (g_key_file_load_from_file(kf, path, 0, error) &&
ensure_keyfile_has_key(kf, "pastebin", "name", error) &&
ensure_keyfile_has_key(kf, "pastebin", "url", error) &&
ensure_keyfile_has_group(kf, "format", error))
{
pastebin = g_malloc(sizeof *pastebin);
pastebin->name = g_key_file_get_string(kf, "pastebin", "name", NULL);
pastebin->config = g_key_file_ref(kf);
}
g_key_file_unref(kf);
return pastebin;
}
static const Pastebin *find_pastebin_by_name(const gchar *name)
{
GSList *node;
g_return_val_if_fail(name != NULL, NULL);
for (node = pastebins; node; node = node->next)
{
Pastebin *pastebin = node->data;
if (strcmp(pastebin->name, name) == 0)
return pastebin;
}
return NULL;
}
static gint sort_pastebins(gconstpointer a, gconstpointer b)
{
return utils_str_casecmp(((Pastebin *) a)->name, ((Pastebin *) b)->name);
}
static void load_pastebins_in_dir(const gchar *path)
{
GError *err = NULL;
GDir *dir = g_dir_open(path, 0, &err);
if (! dir && err->code != G_FILE_ERROR_NOENT)
g_critical("Failed to read directory %s: %s", path, err->message);
if (err)
g_clear_error(&err);
if (dir)
{
const gchar *filename;
while ((filename = g_dir_read_name(dir)))
{
if (*filename == '.') /* silently skip dotfiles */
continue;
else if (! g_str_has_suffix(filename, ".conf"))
{
g_debug("Skipping %s%s%s because it has no .conf extension",
path, G_DIR_SEPARATOR_S, filename);
continue;
}
else
{
gchar *fpath = g_build_filename(path, filename, NULL);
Pastebin *pastebin = pastebin_new(fpath, &err);
if (! pastebin)
{
g_critical("Invalid pastebin configuration file %s: %s",
fpath, err->message);
g_clear_error(&err);
}
else
{
if (! find_pastebin_by_name(pastebin->name))
pastebins = g_slist_prepend(pastebins, pastebin);
else
{
g_debug("Skipping duplicate configuration \"%s\" for "
"pastebin \"%s\"", fpath, pastebin->name);
pastebin_free(pastebin);
}
}
g_free(fpath);
}
}
g_dir_close(dir);
}
}
static void load_all_pastebins(void)
{
gchar *paths[] = {
g_build_filename(geany->app->configdir, "plugins", "geniuspaste",
"pastebins", NULL),
g_build_filename(PLUGINDATADIR, "pastebins", NULL)
};
guint i;
for (i = 0; i < G_N_ELEMENTS(paths); i++)
{
load_pastebins_in_dir(paths[i]);
g_free(paths[i]);
}
pastebins = g_slist_sort(pastebins, sort_pastebins);
}
static void free_all_pastebins(void)
{
g_slist_free_full(pastebins, (GDestroyNotify) pastebin_free);
pastebins = NULL;
}
static void load_settings(void)
{
GKeyFile *config = g_key_file_new();
if (config_file)
g_free(config_file);
config_file = g_strconcat(geany->app->configdir, G_DIR_SEPARATOR_S, "plugins", G_DIR_SEPARATOR_S,
"geniuspaste", G_DIR_SEPARATOR_S, "geniuspaste.conf", NULL);
g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
if (g_key_file_has_key(config, "geniuspaste", "pastebin", NULL) ||
! g_key_file_has_key(config, "geniuspaste", "website", NULL))
{
pastebin_selected = utils_get_setting_string(config, "geniuspaste", "pastebin", "pastebin.geany.org");
}
else
{
/* compatibility for old setting "website" */
switch (utils_get_setting_integer(config, "geniuspaste", "website", 2))
{
case 0: pastebin_selected = g_strdup("codepad.org"); break;
case 1: pastebin_selected = g_strdup("tinypaste.com"); break;
default:
case 2: pastebin_selected = g_strdup("pastebin.geany.org"); break;
case 3: pastebin_selected = g_strdup("dpaste.de"); break;
case 4: pastebin_selected = g_strdup("sprunge.us"); break;
}
}
check_button_is_checked = utils_get_setting_boolean(config, "geniuspaste", "open_browser", FALSE);
author_name = utils_get_setting_string(config, "geniuspaste", "author_name", USERNAME);
g_key_file_free(config);
}
static void save_settings(void)
{
GKeyFile *config = g_key_file_new();
gchar *data;
gchar *config_dir = g_path_get_dirname(config_file);
g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
g_key_file_set_string(config, "geniuspaste", "pastebin", pastebin_selected);
g_key_file_set_boolean(config, "geniuspaste", "open_browser", check_button_is_checked);
g_key_file_set_string(config, "geniuspaste", "author_name", author_name);
if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) && utils_mkdir(config_dir, TRUE) != 0)
{
dialogs_show_msgbox(GTK_MESSAGE_ERROR,
_("Plugin configuration directory could not be created."));
}
else
{
data = g_key_file_to_data(config, NULL, NULL);
utils_write_file(config_file, data);
g_free(data);
}
g_free(config_dir);
g_key_file_free(config);
}
static gchar *get_paste_text(GeanyDocument *doc, gsize *text_len)
{
gsize len;
gchar *paste_text;
if (sci_has_selection(doc->editor->sci))
{
len = sci_get_selected_text_length(doc->editor->sci) + 1;
paste_text = sci_get_selection_contents(doc->editor->sci);
}
else
{
len = sci_get_length(doc->editor->sci) + 1;
paste_text = sci_get_contents(doc->editor->sci, len);
}
if (text_len)
*text_len = len;
return paste_text;
}
static gchar *pastebin_get_default(const Pastebin *pastebin,
const gchar *key,
const gchar *def)
{
return utils_get_setting_string(pastebin->config, "defaults", key, def);
}
static gchar *pastebin_get_language(const Pastebin *pastebin,
const gchar *geany_ft_name)
{
gchar *lang = g_key_file_get_string(pastebin->config, "languages",
geany_ft_name, NULL);
return lang ? lang : pastebin_get_default(pastebin, "language", "");
}
/* append replacement for placeholder @placeholder to @str
* returns %TRUE on success, %FALSE if the placeholder didn't exist
*
* don't prepare replacements because they are unlikely to happen more than
* once for each paste */
static gboolean append_placeholder(GString *str,
const gchar *placeholder,
/* some replacement sources */
const Pastebin *pastebin,
GeanyDocument *doc,
const gchar *contents)
{
/* special builtin placeholders */
if (strcmp("contents", placeholder) == 0)
g_string_append(str, contents);
else if (strcmp("language", placeholder) == 0)
{
gchar *language = pastebin_get_language(pastebin, doc->file_type->name);
g_string_append(str, language);
g_free(language);
}
else if (strcmp("title", placeholder) == 0)
{
gchar *title;
if (doc->file_name)
title = g_path_get_basename(doc->file_name);
else
title = document_get_basename_for_display(doc, -1);
g_string_append(str, title);
g_free(title);
}
else if (strcmp("user", placeholder) == 0)
g_string_append(str, author_name);
/* non-builtins (ones from [defaults] alone) */
else
{
gchar *val = pastebin_get_default(pastebin, placeholder, NULL);
if (val)
{
g_string_append(str, val);
g_free(val);
}
else
{
g_warning("non-existing placeholder \"%%%s%%\"", placeholder);
return FALSE;
}
}
return TRUE;
}
/* expands "%name%" placeholders in @string
*
* placeholders are of the form:
* "%" [a-zA-Z0-9_]+ "%"
* Only valid and supported placeholders are replaced; everything else is left
* as-is. */
static gchar *expand_placeholders(const gchar *string,
const Pastebin *pastebin,
GeanyDocument *doc,
const gchar *contents)
{
GString *str = g_string_new(NULL);
const gchar *p;
for (; (p = strchr(string, '%')); string = p + 1)
{
const gchar *k = p + 1;
gchar *key = NULL;
gsize key_len = 0;
g_string_append_len(str, string, p - string);
while (g_ascii_isalnum(k[key_len]) || k[key_len] == '_')
key_len++;
if (key_len > 0 && k[key_len] == '%')
key = g_strndup(k, key_len);
if (! key)
g_string_append_len(str, p, k + key_len - p);
else if (! append_placeholder(str, key, pastebin, doc, contents))
g_string_append_len(str, p, k + key_len + 1 - p);
if (key) /* skip % suffix too */
key_len++;
g_free(key);
p = k + key_len - 1;
}
g_string_append(str, string);
return g_string_free(str, FALSE);
}
/* Matches @pattern on @haystack and perform match substitutions in @replace */
static gchar *regex_replace(const gchar *pattern,
const gchar *haystack,
const gchar *replace,
GError **error)
{
GRegex *re = g_regex_new(pattern, (G_REGEX_DOTALL |
G_REGEX_DOLLAR_ENDONLY |
G_REGEX_RAW), 0, error);
GMatchInfo *info = NULL;
gchar *result = NULL;
if (re && g_regex_match(re, haystack, 0, &info))
{
GString *str = g_string_new(NULL);
const gchar *p;
for (; (p = strchr(replace, '\\')); replace = p + 1)
{
gint digit = ((gint) p[1]) - '0';
g_string_append_len(str, replace, p - replace);
if (digit >= 0 && digit <= 9 &&
digit < g_match_info_get_match_count(info))
{
gchar *match = g_match_info_fetch(info, digit);
g_string_append(str, match);
g_free(match);
p++;
}
else
g_string_append_c(str, *p);
}
g_string_append(str, replace);
result = g_string_free(str, FALSE);
}
if (info)
g_match_info_free(info);
return result;
}
static void free_data_item(GQuark id, gpointer data, gpointer user_data)
{
g_free(data);
}
/* sends data to @pastebin and returns the raw response */
static SoupMessage *pastebin_soup_message_new(const Pastebin *pastebin,
GeanyDocument *doc,
const gchar *contents)
{
SoupMessage *msg;
gchar *url;
gchar *method;
gsize n_fields;
gchar **fields;
GData *data;
g_return_val_if_fail(pastebin != NULL, NULL);
g_return_val_if_fail(contents != NULL, NULL);
url = utils_get_setting_string(pastebin->config, "pastebin", "url", NULL);
method = utils_get_setting_string(pastebin->config, "pastebin", "method", "POST");
/* prepare the form data */
fields = g_key_file_get_keys(pastebin->config, "format", &n_fields, NULL);
g_datalist_init(&data);
for (gsize i = 0; fields && i < n_fields; i++)
{
gchar *value = g_key_file_get_string(pastebin->config, "format", fields[i], NULL);
SETPTR(value, expand_placeholders(value, pastebin, doc, contents));
g_datalist_set_data(&data, fields[i], value);
}
g_strfreev(fields);
msg = soup_form_request_new_from_datalist(method, url, &data);
g_datalist_foreach(&data, free_data_item, NULL);
g_datalist_clear(&data);
return msg;
}
/* parses @response and returns the URL of the paste, or %NULL on parse error
* or if the URL couldn't be found.
* @warning: it may return NULL even if @error is not set */
static gchar *pastebin_parse_response(const Pastebin *pastebin,
const gchar *response,
GeanyDocument *doc,
const gchar *contents,
GError **error)
{
gchar *url = NULL;
gchar *search;
gchar *replace;
g_return_val_if_fail(pastebin != NULL, NULL);
g_return_val_if_fail(response != NULL, NULL);
search = utils_get_setting_string(pastebin->config, "parse", "search",
"^[[:space:]]*(.+?)[[:space:]]*$");
replace = utils_get_setting_string(pastebin->config, "parse", "replace", "\\1");
SETPTR(replace, expand_placeholders(replace, pastebin, doc, contents));
url = regex_replace(search, response, replace, error);
g_free(search);
g_free(replace);
return url;
}
G_GNUC_PRINTF (4, 5)
static void show_msgbox(GtkMessageType type, GtkButtonsType buttons,
const gchar *main_text,
const gchar *secondary_markup, ...)
{
GtkWidget *dlg;
va_list ap;
gchar *markup;
va_start(ap, secondary_markup);
markup = g_markup_vprintf_escaped(secondary_markup, ap);
va_end(ap);
dlg = g_object_new(GTK_TYPE_MESSAGE_DIALOG,
"message-type", type,
"buttons", buttons,
"transient-for", geany->main_widgets->window,
"modal", TRUE,
"destroy-with-parent", TRUE,
"text", main_text,
"secondary-text", markup,
"secondary-use-markup", TRUE,
NULL);
gtk_dialog_run(GTK_DIALOG(dlg));
gtk_widget_destroy(dlg);
}
static void paste(GeanyDocument * doc, const gchar * website)
{
const Pastebin *pastebin;
gchar *f_content;
gsize f_length;
SoupSession *session;
SoupMessage *msg;
gchar *user_agent = NULL;
guint status;
g_return_if_fail(doc && doc->is_valid);
/* find the pastebin */
pastebin = find_pastebin_by_name(website);
if (! pastebin)
{
show_msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
_("Invalid pastebin service."),
_("Unknown pastebin service \"%s\". "
"Select an existing pastebin service in the preferences "
"or create an appropriate pastebin configuration and "
"retry."),
website);
return;
}
/* get the contents */
f_content = get_paste_text(doc, &f_length);
if (f_content == NULL || f_content[0] == '\0')
{
dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Refusing to create blank paste"));
return;
}
msg = pastebin_soup_message_new(pastebin, doc, f_content);
user_agent = g_strconcat(PLUGIN_NAME, " ", PLUGIN_VERSION, " / Geany ", GEANY_VERSION, NULL);
session = soup_session_async_new_with_options(SOUP_SESSION_USER_AGENT, user_agent, NULL);
g_free(user_agent);
status = soup_session_send_message(session, msg);
g_object_unref(session);
if (! SOUP_STATUS_IS_SUCCESSFUL(status))
{
show_msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
_("Failed to paste the code"),
_("Error pasting the code to the pastebin service %s.\n"
"Error code: %u (%s).\n\n%s"),
pastebin->name, status, msg->reason_phrase,
(SOUP_STATUS_IS_TRANSPORT_ERROR(status)
? _("Check your connection or the pastebin configuration and retry.")
: SOUP_STATUS_IS_SERVER_ERROR(status)
? _("Wait for the service to come back and retry, or retry "
"with another pastebin service.")
: _("Check the pastebin configuration and retry.")));
}
else
{
GError *err = NULL;
gchar *p_url = pastebin_parse_response(pastebin, msg->response_body->data,
doc, f_content, &err);
if (err || ! p_url)
{
show_msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
_("Failed to obtain paste URL."),
_("The code was successfully pasted on %s, but an "
"error occurred trying to obtain its URL: %s\n\n%s"),
pastebin->name,
(err ? err->message
: _("unexpected response from the pastebin service.")),
_("Check the pastebin configuration and retry."));
if (err)
g_error_free(err);
}
else if (check_button_is_checked)
{
utils_open_browser(p_url);
}
else
{
show_msgbox(GTK_MESSAGE_INFO, GTK_BUTTONS_OK,
_("Paste Successful"),
_("Your paste can be found here:\n<a href=\"%s\" "
"title=\"Click to open the paste in your browser\">%s</a>"),
p_url, p_url);
}
g_free(p_url);
}
g_object_unref(msg);
g_free(f_content);
}
static void item_activate(GtkMenuItem * menuitem, gpointer gdata)
{
GeanyDocument *doc = document_get_current();
if(!DOC_VALID(doc))
{
dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("There are no opened documents. Open one and retry.\n"));
return;
}
paste(doc, pastebin_selected);
}
static void on_configure_response(GtkDialog * dialog, gint response, gpointer * user_data)
{
if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY)
{
if(g_strcmp0(gtk_entry_get_text(GTK_ENTRY(widgets.author_entry)), "") == 0)
{
dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("The author name field is empty!"));
}
else
{
SETPTR(pastebin_selected, gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widgets.combo)));
check_button_is_checked = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets.check_button));
SETPTR(author_name, g_strdup(gtk_entry_get_text(GTK_ENTRY(widgets.author_entry))));
save_settings();
}
}
}
GtkWidget *plugin_configure(GtkDialog * dialog)
{
GSList *node;
gint i;
GtkWidget *label, *vbox, *author_label;
vbox = gtk_vbox_new(FALSE, 6);
label = gtk_label_new(_("Select a pastebin:"));
gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
author_label = gtk_label_new(_("Enter the author name:"));
gtk_misc_set_alignment(GTK_MISC(author_label), 0, 0.5);
widgets.author_entry = gtk_entry_new();
if(author_name == NULL)
author_name = g_strdup(USERNAME);
gtk_entry_set_text(GTK_ENTRY(widgets.author_entry), author_name);
widgets.combo = gtk_combo_box_text_new();
for (i = 0, node = pastebins; node; node = node->next, i++)
{
Pastebin *pastebin = node->data;
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(widgets.combo), pastebin->name);
if (pastebin_selected && strcmp(pastebin_selected, pastebin->name) == 0)
gtk_combo_box_set_active(GTK_COMBO_BOX(widgets.combo), i);
}
widgets.check_button = gtk_check_button_new_with_label(_("Show your paste in a new browser tab"));
gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), widgets.combo, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), author_label, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), widgets.author_entry, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), widgets.check_button, FALSE, FALSE, 0);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets.check_button), check_button_is_checked);
gtk_widget_show_all(vbox);
g_signal_connect(dialog, "response", G_CALLBACK(on_configure_response), NULL);
return vbox;
}
static void add_menu_item(void)
{
GtkWidget *paste_item;
paste_item = gtk_menu_item_new_with_mnemonic(_("_Paste it!"));
gtk_widget_show(paste_item);
gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu),
paste_item);
g_signal_connect(paste_item, "activate", G_CALLBACK(item_activate),
NULL);
main_menu_item = paste_item;
}
void plugin_init(GeanyData * data)
{
load_all_pastebins();
load_settings();
main_locale_init(LOCALEDIR, GETTEXT_PACKAGE);
add_menu_item();
}
void plugin_cleanup(void)
{
g_free(author_name);
gtk_widget_destroy(main_menu_item);
free_all_pastebins();
}