static PyObject * Document_get_property(Document *self, const gchar *prop_name) { g_return_val_if_fail(self != NULL, NULL); g_return_val_if_fail(prop_name != NULL, NULL); if (!self->doc) { PyErr_SetString(PyExc_RuntimeError, "Document instance not initialized properly"); return NULL; } if (!DOC_VALID(self->doc)) { PyErr_SetString(PyExc_RuntimeError, "Document is invalid"); return NULL; } if (g_str_equal(prop_name, "basename_for_display")) { PyObject *py_str = NULL; gchar *res_string; res_string = document_get_basename_for_display(self->doc, -1); if (!res_string) { PyErr_SetString(PyExc_RuntimeError, "Geany API failed to return a string"); Py_RETURN_NONE; } py_str = PyString_FromString(res_string); g_free(res_string); return py_str; } else if (g_str_equal(prop_name, "notebook_page")) return Py_BuildValue("i", document_get_notebook_page(self->doc)); else if (g_str_equal(prop_name, "status_color")) { const GdkColor *color = document_get_status_color(self->doc); if (!color) Py_RETURN_NONE; return Py_BuildValue("iii", color->red, color->green, color->blue); } else if (g_str_equal(prop_name, "editor") && self->doc->editor) { Editor *editor; if (self->doc->editor != NULL) { editor = Editor_create_new_from_geany_editor(self->doc->editor); return (PyObject *) editor; } Py_RETURN_NONE; } else if (g_str_equal(prop_name, "encoding") && self->doc->encoding) return PyString_FromString(self->doc->encoding); else if (g_str_equal(prop_name, "file_name") && self->doc->file_name) return PyString_FromString(self->doc->file_name); else if (g_str_equal(prop_name, "file_type") && self->doc->file_type) return (PyObject *) Filetype_create_new_from_geany_filetype(self->doc->file_type); else if (g_str_equal(prop_name, "has_bom")) { if (self->doc->has_bom) Py_RETURN_TRUE; else Py_RETURN_FALSE; } else if (g_str_equal(prop_name, "has_tags")) { if (self->doc->has_tags) Py_RETURN_TRUE; else Py_RETURN_FALSE; } else if (g_str_equal(prop_name, "index")) return Py_BuildValue("i", self->doc->index); else if (g_str_equal(prop_name, "is_valid")) { if (self->doc->is_valid) Py_RETURN_TRUE; else Py_RETURN_FALSE; } else if (g_str_equal(prop_name, "readonly")) { if (self->doc->readonly) Py_RETURN_TRUE; else Py_RETURN_FALSE; } else if (g_str_equal(prop_name, "real_path")) { if (self->doc->real_path) return PyString_FromString(self->doc->real_path); Py_RETURN_NONE; } else if (g_str_equal(prop_name, "text_changed")) { if (self->doc->changed) Py_RETURN_NONE; else Py_RETURN_NONE; } }
static void paste(GeanyDocument * doc, const gchar * website) { SoupSession *session; SoupMessage *msg = NULL; gchar *f_content; gchar const *f_type; gchar *f_title; gchar *p_url; gchar *formdata = NULL; gchar *user_agent = NULL; 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 occ_position; gint i; guint status; gsize f_length; g_return_if_fail(doc && doc->is_valid); f_type = doc->file_type->name; if (doc->file_name == NULL) f_title = document_get_basename_for_display(doc, -1); else f_title = g_path_get_basename(doc->file_name); load_settings(); 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; } 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 = DEFAULT_TYPE_CODEPAD; } msg = soup_message_new("POST", website); formdata = soup_form_encode("lang", f_type, "code", f_content, "submit", "Submit", NULL); break; case TINYPASTE_COM: msg = soup_message_new("POST", website); formdata = soup_form_encode("paste", f_content, "title", f_title, "is_code", g_strcmp0(f_type, "None") == 0 ? "0" : "1", 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; } 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: msg = soup_message_new("POST", website); formdata = soup_form_encode("sprunge", f_content, NULL); break; case PASTEBIN_GEANY_ORG: msg = soup_message_new("POST", website); formdata = soup_form_encode("content", f_content, "author", author_name, "title", f_title, "lexer", f_type, NULL); break; } g_free(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); soup_message_set_request(msg, "application/x-www-form-urlencoded", SOUP_MEMORY_TAKE, formdata, strlen(formdata)); status = soup_session_send_message(session, msg); p_url = g_strdup(msg->response_body->data); g_object_unref(session); g_object_unref(msg); 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) { tokens_array = g_strsplit(p_url, "<a href=\"", 0); /* cuts the string when it finds the first occurrence of '/' * It shoud work even if codepad would change its url. */ SETPTR(p_url, g_strdup(tokens_array[5])); occ_position = indexof(tokens_array[5], '\"'); 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 == TINYPASTE_COM) { /* tinypaste.com returns a XML response which looks * like this: * * <?xml version="1.0" encoding="utf-8"?> * <result> * <response>xxxxx</response> * </result> */ tokens_array = g_strsplit_set(p_url, "<>", 0); SETPTR(p_url, g_strdup_printf("http://%s/%s", websites[TINYPASTE_COM], tokens_array[6])); g_strfreev(tokens_array); } else if(website_selected == DPASTE_DE) { SETPTR(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 */ gchar *ft_tmp = g_ascii_strdown(f_type, -1); g_strstrip(p_url); SETPTR(p_url, g_strdup_printf("%s?%s", p_url, ft_tmp)); g_free(ft_tmp); } if (check_button_is_checked) { utils_open_browser(p_url); } else { GtkWidget *dlg = gtk_message_dialog_new(GTK_WINDOW(geany->main_widgets->window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, _("Paste Successful")); gtk_message_dialog_format_secondary_markup(GTK_MESSAGE_DIALOG(dlg), _("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); gtk_dialog_run(GTK_DIALOG(dlg)); gtk_widget_destroy(dlg); } } else { dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Unable to paste the code. Check your connection and retry.\n" "Error code: %d\n"), status); } g_free(p_url); }