root / trunk / base / ngobject.c @ 1920
History | View | Annotate | Download (6.1 KB)
| 1 | 
                  /* 
                 | 
              
|---|---|
| 2 | 
                  Copyright (C) 2005-2010 Erik van Pienbroek  | 
              
| 3 | 
                   | 
              
| 4 | 
                  This program is free software; you can redistribute it and/or modify  | 
              
| 5 | 
                  it under the terms of the GNU General Public License as published by  | 
              
| 6 | 
                  the Free Software Foundation; either version 2 of the License, or  | 
              
| 7 | 
                  (at your option) any later version.  | 
              
| 8 | 
                   | 
              
| 9 | 
                  This program is distributed in the hope that it will be useful,  | 
              
| 10 | 
                  but WITHOUT ANY WARRANTY; without even the implied warranty of  | 
              
| 11 | 
                  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  | 
              
| 12 | 
                  GNU General Public License for more details.  | 
              
| 13 | 
                   | 
              
| 14 | 
                  You should have received a copy of the GNU General Public License  | 
              
| 15 | 
                  along with this program; if not, write to the Free Software  | 
              
| 16 | 
                  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  | 
              
| 17 | 
                  */  | 
              
| 18 | 
                   | 
              
| 19 | 
                  #ifdef HAVE_CONFIG_H
                 | 
              
| 20 | 
                  #include "config.h"  | 
              
| 21 | 
                  #endif
                 | 
              
| 22 | 
                   | 
              
| 23 | 
                  #include  | 
              
| 24 | 
                  #include  | 
              
| 25 | 
                  #include  | 
              
| 26 | 
                   | 
              
| 27 | 
                  #include "nntpgrab_utils.h"  | 
              
| 28 | 
                   | 
              
| 29 | 
                  #ifdef HAVE_SOUP
                 | 
              
| 30 | 
                  #include  | 
              
| 31 | 
                  #endif
                 | 
              
| 32 | 
                  #ifdef HAVE_SOUP_GNOME
                 | 
              
| 33 | 
                  #include  | 
              
| 34 | 
                  #endif
                 | 
              
| 35 | 
                  #if defined(WIN32) && GLIB_CHECK_VERSION(2,27,1)  | 
              
| 36 | 
                  #include  | 
              
| 37 | 
                  #include  | 
              
| 38 | 
                  #endif
                 | 
              
| 39 | 
                   | 
              
| 40 | 
                  NGTypeInstance* ng_type_check_instance_cast (NGTypeInstance *instance,  | 
              
| 41 | 
                  NGType iface_type)  | 
              
| 42 | 
                  {
                 | 
              
| 43 | 
                      return (NGTypeInstance*) g_type_check_instance_cast((GTypeInstance*) instance, (GType) iface_type);
                 | 
              
| 44 | 
                  }  | 
              
| 45 | 
                   | 
              
| 46 | 
                  #if defined(WIN32) && GLIB_CHECK_VERSION(2,27,1)  | 
              
| 47 | 
                  static gchar *
                 | 
              
| 48 | 
                  get_special_folder (int csidl)
                 | 
              
| 49 | 
                  {
                 | 
              
| 50 | 
                      wchar_t path[MAX_PATH+1];
                 | 
              
| 51 | 
                  HRESULT hr;  | 
              
| 52 | 
                      LPITEMIDLIST pidl = NULL;
                 | 
              
| 53 | 
                  BOOL b;  | 
              
| 54 | 
                      gchar *retval = NULL;
                 | 
              
| 55 | 
                   | 
              
| 56 | 
                      hr = SHGetSpecialFolderLocation (NULL, csidl, &pidl);
                 | 
              
| 57 | 
                      if (hr == S_OK) {
                 | 
              
| 58 | 
                  b = SHGetPathFromIDListW (pidl, path);  | 
              
| 59 | 
                          if (b)
                 | 
              
| 60 | 
                  retval = g_utf16_to_utf8 (path, -1, NULL, NULL, NULL);  | 
              
| 61 | 
                  CoTaskMemFree (pidl);  | 
              
| 62 | 
                  }  | 
              
| 63 | 
                      return retval;
                 | 
              
| 64 | 
                  }  | 
              
| 65 | 
                   | 
              
| 66 | 
                  static void  | 
              
| 67 | 
                  move_win32_configuration_files_if_necessary(void)
                 | 
              
| 68 | 
                  {
                 | 
              
| 69 | 
                      char *appdata_dir = get_special_folder (CSIDL_APPDATA);
                 | 
              
| 70 | 
                  char *config_dir = g_build_path(G_DIR_SEPARATOR_S, appdata_dir, "NNTPGrab", NULL);  | 
              
| 71 | 
                   | 
              
| 72 | 
                      if (g_file_test(config_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
                 | 
              
| 73 | 
                  char *config_dir_new = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), "NNTPGrab", NULL);  | 
              
| 74 | 
                  g_rename(config_dir, config_dir_new);  | 
              
| 75 | 
                  g_free(config_dir_new);  | 
              
| 76 | 
                  }  | 
              
| 77 | 
                   | 
              
| 78 | 
                  g_free(config_dir);  | 
              
| 79 | 
                  g_free(appdata_dir);  | 
              
| 80 | 
                  }  | 
              
| 81 | 
                  #endif
                 | 
              
| 82 | 
                   | 
              
| 83 | 
                  #ifdef _MSC_VER
                 | 
              
| 84 | 
                  static void AppInvalidParameterHandler(const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t pReserved)  | 
              
| 85 | 
                  {
                 | 
              
| 86 | 
                      fprintf(stderr, "Invalid parameter detected in function %s. File: %s Line: %d\n", function, file, line);
                 | 
              
| 87 | 
                      fprintf(stderr, "Expression: %s\n", expression);
                 | 
              
| 88 | 
                   | 
              
| 89 | 
                      /* Cause a Debug Breakpoint */
                 | 
              
| 90 | 
                  DebugBreak();  | 
              
| 91 | 
                  }  | 
              
| 92 | 
                  #endif
                 | 
              
| 93 | 
                   | 
              
| 94 | 
                  void nntpgrab_utils_perform_base_initialization(void)  | 
              
| 95 | 
                  {
                 | 
              
| 96 | 
                  #if defined(WIN32) || defined(DARWIN)
                 | 
              
| 97 | 
                      char *dirname;
                 | 
              
| 98 | 
                      char *dirname2;
                 | 
              
| 99 | 
                  #endif
                 | 
              
| 100 | 
                  #ifdef WIN32
                 | 
              
| 101 | 
                  WORD wVersionRequested;  | 
              
| 102 | 
                  WSADATA wsaData;  | 
              
| 103 | 
                      int ret;
                 | 
              
| 104 | 
                   | 
              
| 105 | 
                  #ifdef _MSC_VER
                 | 
              
| 106 | 
                  _set_invalid_parameter_handler(AppInvalidParameterHandler);  | 
              
| 107 | 
                  #else
                 | 
              
| 108 | 
                      /* exchndl.dll is part of Dr. MinGW. By loading this DLL, every time the 
                 | 
              
| 109 | 
                  * program crashes a .RPT file will be created containing the backtrace */  | 
              
| 110 | 
                      LoadLibraryA("exchndl.dll");
                 | 
              
| 111 | 
                  #endif
                 | 
              
| 112 | 
                   | 
              
| 113 | 
                      /* Initialise Winsock */
                 | 
              
| 114 | 
                  wVersionRequested = MAKEWORD(2, 2);  | 
              
| 115 | 
                   | 
              
| 116 | 
                  ret = WSAStartup(wVersionRequested, &wsaData);  | 
              
| 117 | 
                  if (ret != 0) {  | 
              
| 118 | 
                          /* Tell the user that we could not find a usable */
                 | 
              
| 119 | 
                          /* WinSock DLL. */
                 | 
              
| 120 | 
                          g_error(_("Winsock could not be initialised"));
                 | 
              
| 121 | 
                          return;
                 | 
              
| 122 | 
                  }  | 
              
| 123 | 
                   | 
              
| 124 | 
                      /* Confirm that the WinSock DLL supports 2.2. */
                 | 
              
| 125 | 
                      /* Note that if the DLL supports versions greater */
                 | 
              
| 126 | 
                      /* than 2.2 in addition to 2.2, it will still return */
                 | 
              
| 127 | 
                      /* 2.2 in wVersion since that is the version we */
                 | 
              
| 128 | 
                      /* requested. */
                 | 
              
| 129 | 
                   | 
              
| 130 | 
                  if (LOBYTE( wsaData.wVersion ) != 2 ||  | 
              
| 131 | 
                          HIBYTE( wsaData.wVersion ) != 2) {
                 | 
              
| 132 | 
                          /* Tell the user that we could not find a usable */
                 | 
              
| 133 | 
                          /* WinSock DLL. */
                 | 
              
| 134 | 
                  WSACleanup();  | 
              
| 135 | 
                   | 
              
| 136 | 
                          g_error(_("Winsock 2.2 or higher is required for this program"));
                 | 
              
| 137 | 
                   | 
              
| 138 | 
                          return;
                 | 
              
| 139 | 
                  }  | 
              
| 140 | 
                  #endif
                 | 
              
| 141 | 
                   | 
              
| 142 | 
                  if (!g_thread_supported ()) g_thread_init (NULL);  | 
              
| 143 | 
                  g_type_init();  | 
              
| 144 | 
                   | 
              
| 145 | 
                  #ifdef DARWIN
                 | 
              
| 146 | 
                  dirname = g_get_current_dir();  | 
              
| 147 | 
                  dirname2 = g_build_path(G_DIR_SEPARATOR_S, dirname, "../share/locale", NULL);  | 
              
| 148 | 
                  bindtextdomain (GETTEXT_PACKAGE, dirname2);  | 
              
| 149 | 
                  g_free(dirname2);  | 
              
| 150 | 
                  g_free(dirname);  | 
              
| 151 | 
                  #elif defined(WIN32)
                 | 
              
| 152 | 
                      dirname = g_win32_get_package_installation_directory_of_module(NULL);
                 | 
              
| 153 | 
                  dirname2 = g_build_path(G_DIR_SEPARATOR_S, dirname, "\\share\\locale", NULL);  | 
              
| 154 | 
                  bindtextdomain (GETTEXT_PACKAGE, dirname2);  | 
              
| 155 | 
                  g_free(dirname2);  | 
              
| 156 | 
                  #else
                 | 
              
| 157 | 
                  bindtextdomain (GETTEXT_PACKAGE, NNTPGRABLOCALEDIR);  | 
              
| 158 | 
                  #endif
                 | 
              
| 159 | 
                      bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
                 | 
              
| 160 | 
                  textdomain (GETTEXT_PACKAGE);  | 
              
| 161 | 
                   | 
              
| 162 | 
                  #ifdef HAVE_SOUP
                 | 
              
| 163 | 
                      /* Due to a bug in libsoup we need to make sure that all GObject's 
                 | 
              
| 164 | 
                  * are initialized from the main thread to avoid a deadlock when libsoup  | 
              
| 165 | 
                  * is called for the first time from a different thread */  | 
              
| 166 | 
                  SoupSession *session = soup_session_sync_new();  | 
              
| 167 | 
                   | 
              
| 168 | 
                  #if 0 
                 | 
              
| 169 | 
                  SoupLogger *logger = soup_logger_new(SOUP_LOGGER_LOG_BODY, -1);  | 
              
| 170 | 
                  soup_session_add_feature(session, SOUP_SESSION_FEATURE(logger));  | 
              
| 171 | 
                  g_object_unref(logger);  | 
              
| 172 | 
                  #endif  | 
              
| 173 | 
                   | 
              
| 174 | 
                  #ifdef HAVE_SOUP_GNOME
                 | 
              
| 175 | 
                      /* Enable proxy support when using GNOME 2.26.0 or higher */
                 | 
              
| 176 | 
                  soup_session_add_feature_by_type(session, SOUP_TYPE_GNOME_FEATURES_2_26);  | 
              
| 177 | 
                  #endif
                 | 
              
| 178 | 
                   | 
              
| 179 | 
                  #ifdef HAVE_SOUP_GZIP
                 | 
              
| 180 | 
                      /* Enable gzip compression */
                 | 
              
| 181 | 
                  soup_session_add_feature_by_type(session, SOUP_TYPE_CONTENT_DECODER);  | 
              
| 182 | 
                  #endif
                 | 
              
| 183 | 
                   | 
              
| 184 | 
                  g_object_unref(session);  | 
              
| 185 | 
                  #endif
                 | 
              
| 186 | 
                   | 
              
| 187 | 
                  #if defined(WIN32) && GLIB_CHECK_VERSION(2,27,1)  | 
              
| 188 | 
                      /* As of GLib 2.27.1 the return value of the function g_get_user_data_dir() 
                 | 
              
| 189 | 
                  * was changed to return CSIDL_LOCAL_APPDATA instead of CSIDL_APPDATA on Win32.  | 
              
| 190 | 
                  * To prevent a broken NNTPGrab configuration, check if there are configuration  | 
              
| 191 | 
                  * files at the old path and move them to the new location if necessary */  | 
              
| 192 | 
                  move_win32_configuration_files_if_necessary();  | 
              
| 193 | 
                  #endif
                 | 
              
| 194 | 
                  }  | 
              
NNTPGrab

