Branch: refs/heads/master Author: Frank Lanitz frank@frank.uvena.de Committer: Frank Lanitz frank@frank.uvena.de Date: Sun, 06 May 2012 07:48:05 Commit: 6736add0a4b7527f8fea33c9b2b4c98f98f20599 https://github.com/geany/geany-plugins/commit/6736add0a4b7527f8fea33c9b2b4c9...
Log Message: ----------- GeniusPaste: Some improvements
Modified Paths: -------------- geniuspaste/TODO geniuspaste/src/geniuspaste.c
Modified: geniuspaste/TODO 4 files changed, 3 insertions(+), 1 deletions(-) =================================================================== @@ -1,4 +1,6 @@ TODO ----
- * Make the geany's pastebin the default pastebin + [!] Implement saving/loading of the settings + [-] Make the geany's pastebin the default pastebin +
Modified: geniuspaste/src/geniuspaste.c 384 files changed, 190 insertions(+), 194 deletions(-) =================================================================== @@ -24,17 +24,15 @@ #include "string.h"
#ifdef HAVE_CONFIG_H - #include "config.h" /* for the gettext domain */ +#include "config.h" /* for the gettext domain */ #endif
#include <geanyplugin.h>
#ifdef G_OS_WIN32 - #define OPEN_BROWSER "start" - #define USERNAME getenv("USERNAME") +#define USERNAME getenv("USERNAME") #else - #define OPEN_BROWSER "xdg-open" - #define USERNAME getenv("USER") +#define USERNAME getenv("USER") #endif
#define CODEPAD_ORG 0 @@ -43,6 +41,9 @@ #define DPASTE_DE 3 #define SPRUNGE_US 4
+#define DEFAULT_TYPE_CODEPAD langs_supported_codepad[8]; +#define DEFAULT_TYPE_DPASTE langs_supported_dpaste[15]; + GeanyPlugin *geany_plugin; GeanyData *geany_data; GeanyFunctions *geany_functions; @@ -62,41 +63,46 @@ GtkWidget *check_button; } widgets;
- -static gint websites_num = 5; static gint website_selected; -static gchar *welcome_text = NULL; static gboolean check_button_is_checked = FALSE;
PLUGIN_VERSION_CHECK(147) PLUGIN_SET_TRANSLATABLE_INFO(LOCALEDIR, GETTEXT_PACKAGE, "GeniusPaste", - _("Paste your code on your favorite pastebin"), + _("Paste your code on your favorite pastebin"), "0.1", "Enrico Trotta");
+static gint indexof(const gchar * string, gchar c) +{ + gchar * occ = strchr(string, c); + return occ ? occ - string : -1; +}
-static gint last_indexof(gchar * string, gchar c) +static gint last_indexof(const gchar * string, gchar c) { - gchar * occ = strrchr(string, c); - return occ ? strrchr(string, c) - string : -1; + gchar * occ = strrchr(string, c); + return occ ? occ - string : -1; }
static void paste(const gchar * website) { - g_type_init(); - SoupSession *session = soup_session_async_new(); SoupMessage *msg = NULL;
GeanyDocument *doc = document_get_current(); + + if(doc == NULL) { + dialogs_show_msgbox(GTK_MESSAGE_ERROR, "There are no opened documents. Open one and retry.\n"); + return; + } + GeanyFiletype *ft = doc->file_type;
GError *error;
- gchar *buffer_shell_command; gchar *f_content; gchar *f_type = g_strdup(ft->name); gchar *f_path = doc->real_path; - gchar *f_name = doc->file_name; + gchar *f_name = doc->file_name; gchar *f_title; gchar *p_url; gchar *formdata = NULL; @@ -104,18 +110,18 @@ static void paste(const gchar * website) gchar **tokens_array;
const gchar *langs_supported_codepad[] = { "C", "C++", "D", "Haskell", - "Lua", "OCaml", "PHP", "Perl", "Plain Text", - "Python", "Ruby", "Scheme", "Tcl" + "Lua", "OCaml", "PHP", "Perl", "Plain Text", + "Python", "Ruby", "Scheme", "Tcl" };
const gchar *langs_supported_dpaste[] = { "Bash", "C", "CSS", "Diff", - "Django/Jinja", "HTML", "IRC logs", "JavaScript", "PHP", - "Python console session", "Python Traceback", "Python", - "Python3", "Restructured Text", "SQL", "Text only" + "Django/Jinja", "HTML", "IRC logs", "JavaScript", "PHP", + "Python console session", "Python Traceback", "Python", + "Python3", "Restructured Text", "SQL", "Text only" };
- gint i; gint occ_position; + gint i; guint status; gsize f_lenght; gboolean result; @@ -125,170 +131,168 @@ static void paste(const gchar * website)
switch (website_selected) {
- case CODEPAD_ORG: - - for (i = 0; i < G_N_ELEMENTS(langs_supported_codepad); i++) { - if (g_strcmp0(f_type, langs_supported_codepad[i]) == 0) - break; - else - f_type = langs_supported_codepad[8]; - } - - if ((result = - g_file_get_contents(f_path, &f_content, &f_lenght, - &error)) == FALSE) { - dialogs_show_msgbox(GTK_MESSAGE_ERROR, - "Unable to the the content of the file"); - g_error_free(error); - } - - msg = soup_message_new("POST", website); - formdata = - soup_form_encode("lang", f_type, "code", f_content, "submit", - "Submit", NULL); - break; - - case PASTEBIN_COM: - - if ((result = - g_file_get_contents(f_path, &f_content, &f_lenght, - &error)) == FALSE) { - dialogs_show_msgbox(GTK_MESSAGE_ERROR, - "Unable to the the content of the file"); - g_error_free(error); - } - - msg = soup_message_new("POST", website); - formdata = - soup_form_encode("paste_code", f_content, "paste_format", - f_type, "paste_name", f_title, NULL); - break; - - - case DPASTE_DE: - - for (i = 0; i < G_N_ELEMENTS(langs_supported_dpaste); i++) { - if (g_strcmp0(f_type, langs_supported_dpaste[i]) == 0) - break; - else - f_type = langs_supported_dpaste[15]; // Text only - } - - if ((result = - g_file_get_contents(f_path, &f_content, &f_lenght, - &error)) == FALSE) { - dialogs_show_msgbox(GTK_MESSAGE_ERROR, - "Unable to the the content of the file"); - g_error_free(error); - } - - msg = soup_message_new("POST", website); - formdata = - soup_form_encode("content", f_content, "title", f_title, - "lexer", f_type, NULL); - - break; - - case SPRUNGE_US: - - if ((result = - g_file_get_contents(f_path, &f_content, &f_lenght, - &error)) == FALSE) { - dialogs_show_msgbox(GTK_MESSAGE_ERROR, - "Unable to the the content of the file"); - g_error_free(error); - } - - msg = soup_message_new("POST", website); - formdata = - soup_form_encode("sprunge", f_content, NULL); - - break; - - case PASTEBIN_GEANY_ORG: - - if ((result = - g_file_get_contents(f_path, &f_content, &f_lenght, - &error)) == FALSE) { - dialogs_show_msgbox(GTK_MESSAGE_ERROR, - "Unable to the the content of the file"); - g_error_free(error); - } - - msg = soup_message_new("POST", website); - formdata = - soup_form_encode("content", f_content, "author", USERNAME, - "title", f_title, "lexer", f_type, NULL); - - break; + case CODEPAD_ORG: + + for (i = 0; i < G_N_ELEMENTS(langs_supported_codepad); i++) { + if (g_strcmp0(f_type, langs_supported_codepad[i]) == 0) + break; + else + f_type = DEFAULT_TYPE_CODEPAD; + } + + result = g_file_get_contents(f_path, &f_content, &f_lenght, &error); + if(result == FALSE) { + dialogs_show_msgbox(GTK_MESSAGE_ERROR, "Unable to the the content of the file"); + g_error_free(error); + return; + } + + msg = soup_message_new("POST", website); + formdata = soup_form_encode("lang", f_type, "code", f_content, + "submit", "Submit", NULL); + + break; + + case PASTEBIN_COM: + + result = g_file_get_contents(f_path, &f_content, &f_lenght, &error); + if(result == FALSE) { + dialogs_show_msgbox(GTK_MESSAGE_ERROR, "Unable to the the content of the file"); + g_error_free(error); + return; + } + + msg = soup_message_new("POST", website); + formdata = soup_form_encode("paste_code", f_content, "paste_format", + f_type, "paste_name", f_title, NULL); + + break; + + + case DPASTE_DE: + + for (i = 0; i < G_N_ELEMENTS(langs_supported_dpaste); i++) { + if (g_strcmp0(f_type, langs_supported_dpaste[i]) == 0) + break; + else + f_type = DEFAULT_TYPE_DPASTE; + } + + result = g_file_get_contents(f_path, &f_content, &f_lenght, &error); + if(result == FALSE) { + dialogs_show_msgbox(GTK_MESSAGE_ERROR, "Unable to the the content of the file"); + g_error_free(error); + return; + } + + msg = soup_message_new("POST", website); + /* apparently dpaste.de detects automatically the syntax of the + * pasted code so 'lexer' should be unneeded + */ + formdata = soup_form_encode("content", f_content, "title", f_title, + "lexer", f_type, NULL); + + break; + + case SPRUNGE_US: + + result = g_file_get_contents(f_path, &f_content, &f_lenght, &error); + if(result == FALSE) { + dialogs_show_msgbox(GTK_MESSAGE_ERROR, "Unable to the the content of the file"); + g_error_free(error); + return; + } + + msg = soup_message_new("POST", website); + formdata = soup_form_encode("sprunge", f_content, NULL); + + break; + + case PASTEBIN_GEANY_ORG: + + result = g_file_get_contents(f_path, &f_content, &f_lenght, &error); + if(result == FALSE) { + dialogs_show_msgbox(GTK_MESSAGE_ERROR, "Unable to the the content of the file"); + g_error_free(error); + return; + } + + msg = soup_message_new("POST", website); + formdata = soup_form_encode("content", f_content, "author", USERNAME, + "title", f_title, "lexer", f_type, NULL); + + break;
}
soup_message_set_request(msg, "application/x-www-form-urlencoded", - SOUP_MEMORY_COPY, formdata, strlen(formdata)); + SOUP_MEMORY_COPY, formdata, strlen(formdata));
status = soup_session_send_message(session, msg); - - if(status == 200) { - - p_url = msg->response_body->data; - - /* - * codepad.org doesn't return only the url of the new snippet pasted - * but an html page. This minimal parser will get the bare url. - */ - - if (website_selected == CODEPAD_ORG) { - temp_body = g_strdup(p_url); - tokens_array = g_strsplit(temp_body, "<a href="", 0); - - /* copy the first 27 chars because the url is composed by 27 characters. - * codepad.org/XXXXXXXX - */ - p_url = g_strndup(tokens_array[5], 27); - - g_free(temp_body); - g_strfreev(tokens_array); - - } else if(website_selected == DPASTE_DE) { - p_url = g_strndup(p_url + 1, strlen(p_url) - 2); - - } else if(website_selected == SPRUNGE_US) { - - /* in order to enable the syntax highlightning on sprunge.us - * it is necessary to append at the returned url a question - * mark '?' followed by the file type. - * - * e.g. sprunge.us/xxxx?c - */ - p_url[strlen(p_url) - 1] = '\0'; - f_type[0] = g_ascii_tolower(f_type[0]); - temp_body = g_strdup_printf("?%s", f_type); - g_strlcat(p_url + 1, temp_body, -1); - p_url = g_strchomp(p_url); - free(temp_body); - } - - if (check_button_is_checked) { - buffer_shell_command = g_malloc(100 * sizeof(gchar)); - sprintf(buffer_shell_command, "%s %s", OPEN_BROWSER, p_url); - system(buffer_shell_command); - g_free(buffer_shell_command); - } else { - dialogs_show_msgbox(GTK_MESSAGE_INFO, "%s", p_url); - } - - if(website_selected == CODEPAD_ORG || website_selected == DPASTE_DE) { - g_free(p_url); - } + p_url = g_strdup(msg->response_body->data); + g_free(f_content); + + if(status == SOUP_STATUS_OK) { + + /* + * codepad.org doesn't return only the url of the new snippet pasted + * but an html page. This minimal parser will get the bare url. + */ + + if (website_selected == CODEPAD_ORG) { + temp_body = g_strdup(p_url); + tokens_array = g_strsplit(temp_body, "<a href="", 0); + + /* cuts the string when it finds the first occurrence of '/' + * It shoud work even if codepad would change its url. + */ + + p_url = g_strdup(tokens_array[5]); + occ_position = indexof(tokens_array[5], '"'); + + g_free(temp_body); + g_strfreev(tokens_array); + + if(occ_position != -1) { + p_url[occ_position] = '\0'; + } else { + dialogs_show_msgbox(GTK_MESSAGE_ERROR, "Unable to paste the code on codepad.org\n" + "Retry or select another pastebin."); + g_free(p_url); + return; + } + + } else if(website_selected == DPASTE_DE) { + p_url = g_strndup(p_url + 1, strlen(p_url) - 2); + + } else if(website_selected == SPRUNGE_US) { + + /* in order to enable the syntax highlightning on sprunge.us + * it is necessary to append at the returned url a question + * mark '?' followed by the file type. + * + * e.g. sprunge.us/xxxx?c + */ + p_url[strlen(p_url) - 1] = '\0'; + f_type[0] = g_ascii_tolower(f_type[0]); + temp_body = g_strdup_printf("?%s", f_type); + g_strlcat(p_url + 1, temp_body, -1); + p_url = g_strchomp(p_url); + g_free(temp_body); + } + + if (check_button_is_checked) { + utils_open_browser(p_url); + } else { + dialogs_show_msgbox(GTK_MESSAGE_INFO, "%s", p_url); + }
} else { - dialogs_show_msgbox(GTK_MESSAGE_INFO, "Unable to paste the code. Check your connection and retry.\n" - "Error code: %d\n", status); + dialogs_show_msgbox(GTK_MESSAGE_ERROR, "Unable to paste the code. Check your connection and retry.\n" + "Error code: %d\n", status); }
- g_free(f_content); - + g_free(p_url); }
static void item_activate(GtkMenuItem * menuitem, gpointer gdata) @@ -299,14 +303,11 @@ static void item_activate(GtkMenuItem * menuitem, gpointer gdata) static void on_configure_response(GtkDialog * dialog, gint response, gpointer * user_data) { if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY) { + website_selected = gtk_combo_box_get_active(GTK_COMBO_BOX(widgets.combo));
- website_selected = - gtk_combo_box_get_active - (GTK_COMBO_BOX(widgets.combo)); - - if (gtk_toggle_button_get_active - (GTK_TOGGLE_BUTTON(widgets.check_button))) - check_button_is_checked = TRUE; + if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets.check_button))) { + check_button_is_checked = TRUE; + } } }
@@ -322,15 +323,12 @@ GtkWidget *plugin_configure(GtkDialog * dialog)
widgets.combo = gtk_combo_box_text_new();
- for (i = 0; i < websites_num; i++) - gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(widgets.combo), - websites[i]); + for (i = 0; i < G_N_ELEMENTS(websites); i++) + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(widgets.combo), websites[i]);
gtk_combo_box_set_active(GTK_COMBO_BOX(widgets.combo), 0);
- widgets.check_button = - gtk_check_button_new_with_label - ("show your paste in a new browser tab"); + widgets.check_button = gtk_check_button_new_with_label("show your paste in a new browser tab");
gtk_container_add(GTK_CONTAINER(vbox), label); gtk_container_add(GTK_CONTAINER(vbox), widgets.combo); @@ -338,8 +336,7 @@ GtkWidget *plugin_configure(GtkDialog * dialog)
gtk_widget_show_all(vbox);
- g_signal_connect(dialog, "response", G_CALLBACK(on_configure_response), - NULL); + g_signal_connect(dialog, "response", G_CALLBACK(on_configure_response), NULL);
return vbox; } @@ -351,9 +348,9 @@ static void add_menu_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); + paste_item); g_signal_connect(paste_item, "activate", G_CALLBACK(item_activate), - NULL); + NULL);
main_menu_item = paste_item; } @@ -367,5 +364,4 @@ void plugin_init(GeanyData * data) void plugin_cleanup(void) { gtk_widget_destroy(main_menu_item); - g_free(welcome_text); }
@@ Diff output truncated at 100000 characters. @@
-------------- This E-Mail was brought to you by github_commit_mail.py (Source: TBD).
plugins-commits@lists.geany.org