/* * This file part of StarDict - A international dictionary for GNOME. * http://stardict.sourceforge.net * Copyright (C) 2005-2006 Evgeniy * * 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 3 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef HAVE_CONFIG_H # include "config.h" #endif #include #include #include #include #include #ifdef CONFIG_GNOME # include # include #elif defined(_WIN32) # include #endif #include "utils.h" void ProcessGtkEvent() { while (gtk_events_pending()) gtk_main_iteration(); } std::string get_user_config_dir() { const gchar *config_path_from_env = g_getenv("STARDICT_CONFIG_PATH"); if (config_path_from_env) return config_path_from_env; #ifdef _WIN32 std::string res = g_get_user_config_dir(); res += G_DIR_SEPARATOR_S "StarDict"; return res; #else std::string res; gchar *tmp = g_build_filename(g_get_home_dir(), ".stardict", NULL); res=tmp; g_free(tmp); return res; #endif } std::string combnum2str(gint comb_code) { switch (comb_code) { #ifdef _WIN32 case 0: return "Shift"; case 1: return "Alt"; case 2: return "Ctrl"; case 3: return "Ctrl+Alt"; #else case 0: return "Win"; case 1: return "Shift"; case 2: return "Alt"; case 3: return "Ctrl"; case 4: return "Ctrl+Alt"; case 5: return "Ctrl+e"; case 6: return "Win+d"; case 7: return "F1"; case 8: return "F2"; case 9: return "F3"; case 10: return "F4"; #endif default: return ""; } } std::vector split(const std::string& str, char sep) { std::vector res; std::string::size_type prev_pos=0, pos = 0; while ((pos=str.find(sep, prev_pos))!=std::string::npos) { res.push_back(std::string(str, prev_pos, pos-prev_pos)); prev_pos=pos+1; } res.push_back(std::string(str, prev_pos, str.length()-prev_pos)); return res; } GdkPixbuf *load_image_from_file(const std::string& filename) { GError *err=NULL; GdkPixbuf *res=gdk_pixbuf_new_from_file(filename.c_str(), &err); if (!res) { GtkWidget *message_dlg = gtk_message_dialog_new( NULL, (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Can not load image. %s"), err->message); gtk_dialog_set_default_response(GTK_DIALOG(message_dlg), GTK_RESPONSE_OK); gtk_window_set_resizable(GTK_WINDOW(message_dlg), FALSE); gtk_dialog_run(GTK_DIALOG(message_dlg)); gtk_widget_destroy(message_dlg); g_error_free(err); exit(EXIT_FAILURE); } return res; } static gchar * byte_to_hex(unsigned char nr) { gchar *result = NULL; result = g_strdup_printf("%%%x%x", nr / 0x10, nr % 0x10); return result; } char *common_encode_uri_string(const char *string) { gchar *newURIString; gchar *hex, *tmp = NULL; int i, j, len, bytes; /* the UTF-8 string is casted to ASCII to treat the characters bytewise and convert non-ASCII compatible chars to URI hexcodes */ newURIString = g_strdup(""); len = strlen(string); for(i = 0; i < len; i++) { if(g_ascii_isalnum(string[i]) || strchr("-_.!~*'()", (int)string[i])) tmp = g_strdup_printf("%s%c", newURIString, string[i]); else if(string[i] == ' ') tmp = g_strdup_printf("%s%%20", newURIString); else if((unsigned char)string[i] <= 127) { tmp = g_strdup_printf("%s%s", newURIString, hex = byte_to_hex(string[i]));g_free(hex); } else { bytes = 0; if(((unsigned char)string[i] >= 192) && ((unsigned char)string[i] <= 223)) bytes = 2; else if(((unsigned char)string[i] > 223) && ((unsigned char)string[i] <= 239)) bytes = 3; else if(((unsigned char)string[i] > 239) && ((unsigned char)string[i] <= 247)) bytes = 4; else if(((unsigned char)string[i] > 247) && ((unsigned char)string[i] <= 251)) bytes = 5; else if(((unsigned char)string[i] > 247) && ((unsigned char)string[i] <= 251)) bytes = 6; if(0 != bytes) { if((i + (bytes - 1)) > len) { g_warning(("Unexpected end of character sequence or corrupt UTF-8 encoding! Some characters were dropped!")); break; } for(j=0; j < (bytes - 1); j++) { tmp = g_strdup_printf("%s%s", newURIString, hex = byte_to_hex((unsigned char)string[i++])); g_free(hex); g_free(newURIString); newURIString = tmp; } tmp = g_strdup_printf("%s%s", newURIString, hex = byte_to_hex((unsigned char)string[i])); g_free(hex); } else { /* sh..! */ g_error("Internal error while converting UTF-8 chars to HTTP URI!"); } } g_free(newURIString); newURIString = tmp; } return newURIString; }