[geany/geany-plugins] 567b5c: added handling of errors, overall clean of the code, removed uneeded code
Enrico
git-noreply at xxxxx
Wed May 2 20:15:35 UTC 2012
Branch: refs/heads/master
Author: Enrico <enrico.trt at gmail.com>
Committer: Enrico <enrico.trt at gmail.com>
Date: Wed, 02 May 2012 20:15:35
Commit: 567b5c65716f57e90a6bc9241a674b0d3780966d
https://github.com/geany/geany-plugins/commit/567b5c65716f57e90a6bc9241a674b0d3780966d
Log Message:
-----------
added handling of errors, overall clean of the code, removed uneeded code
Modified Paths:
--------------
geniuspaste/src/geniuspaste.c
Modified: geniuspaste/src/geniuspaste.c
178 files changed, 71 insertions(+), 107 deletions(-)
===================================================================
@@ -30,10 +30,8 @@
#include <geanyplugin.h>
#ifdef G_OS_WIN32
- #define OPEN_BROWSER "start"
#define USERNAME getenv("USERNAME")
#else
- #define OPEN_BROWSER "xdg-open"
#define USERNAME getenv("USER")
#endif
@@ -62,10 +60,7 @@
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)
@@ -74,29 +69,37 @@
"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;
+ 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_type = g_strdup(ft->name);
+ gchar *f_path = doc->real_path;
gchar *f_name = doc->file_name;
gchar *f_title;
gchar *p_url;
@@ -104,18 +107,6 @@ static void paste(const gchar * website)
gchar *temp_body;
gchar **tokens_array;
- const gchar *langs_supported_codepad[] = { "C", "C++", "D", "Haskell",
- "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"
- };
-
- gint i;
gint occ_position;
guint status;
gsize f_lenght;
@@ -123,116 +114,97 @@ static void paste(const gchar * website)
occ_position = last_indexof(f_name, G_DIR_SEPARATOR);
f_title = f_name + occ_position + 1;
-
+
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);
+
+ 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);
+ 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");
+ 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);
+ 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");
+ printf("%s\n", f_type);
+ 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, "title", f_title,
- "lexer", f_type, NULL);
+ /* 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:
- 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");
+ 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);
+ 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");
+ 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);
+ formdata = soup_form_encode("content", f_content, "author", USERNAME,
+ "title", f_title, "lexer", f_type, NULL);
- break;
+ 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) {
+ if(status == SOUP_STATUS_OK) {
- p_url = msg->response_body->data;
+ p_url = g_strdup(msg->response_body->data);
/*
* codepad.org doesn't return only the url of the new snippet pasted
@@ -243,11 +215,13 @@ static void paste(const gchar * website)
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
+ /* cuts the string when it finds the first occurrence of '/'
+ * It shoud work even if codepad would change its url.
*/
- p_url = g_strndup(tokens_array[5], 27);
-
+
+ p_url = g_strdup(tokens_array[5]);
+ p_url[indexof(tokens_array[5], '\"')] = '\0';
+
g_free(temp_body);
g_strfreev(tokens_array);
@@ -267,24 +241,18 @@ static void paste(const gchar * website)
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);
+ g_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);
+ utils_open_browser(p_url);
+ g_free(p_url);
} else {
dialogs_show_msgbox(GTK_MESSAGE_INFO, "%s", p_url);
}
-
- if(website_selected == CODEPAD_ORG || website_selected == DPASTE_DE) {
- g_free(p_url);
- }
} else {
- dialogs_show_msgbox(GTK_MESSAGE_INFO, "Unable to paste the code. Check your connection and retry.\n"
+ dialogs_show_msgbox(GTK_MESSAGE_ERROR, "Unable to paste the code. Check your connection and retry.\n"
"Error code: %d\n", status);
}
@@ -300,14 +268,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;
+ }
}
}
@@ -323,7 +288,7 @@ GtkWidget *plugin_configure(GtkDialog * dialog)
widgets.combo = gtk_combo_box_text_new();
- for (i = 0; i < websites_num; i++)
+ for (i = 0; i < G_N_ELEMENTS(websites); i++)
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(widgets.combo),
websites[i]);
@@ -368,5 +333,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).
More information about the Plugins-Commits
mailing list