Statistics
| Revision:

root / trunk / base / ngobject.c @ 1919

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
    WORD wVersionRequested;
100
    WSADATA wsaData;
101
    int ret;
102

                
103
#ifdef _MSC_VER
104
    _set_invalid_parameter_handler(AppInvalidParameterHandler);
105
#else
106
    /* exchndl.dll is part of Dr. MinGW. By loading this DLL, every time the 
107
     * program crashes a .RPT file will be created containing the backtrace */
108
    LoadLibraryA("exchndl.dll");
109
#endif
110

                
111
    /* Initialise Winsock */
112
    wVersionRequested = MAKEWORD(2, 2);
113

                
114
    ret = WSAStartup(wVersionRequested, &wsaData);
115
    if (ret != 0) {
116
        /* Tell the user that we could not find a usable */
117
        /* WinSock DLL. */
118
        g_error(_("Winsock could not be initialised"));
119
        return;
120
    }
121

                
122
    /* Confirm that the WinSock DLL supports 2.2. */
123
    /* Note that if the DLL supports versions greater */
124
    /* than 2.2 in addition to 2.2, it will still return */
125
    /* 2.2 in wVersion since that is the version we */
126
    /* requested. */
127

                
128
    if (LOBYTE( wsaData.wVersion ) != 2 ||
129
        HIBYTE( wsaData.wVersion ) != 2) {
130
        /* Tell the user that we could not find a usable */
131
        /* WinSock DLL. */
132
        WSACleanup();
133

                
134
        g_error(_("Winsock 2.2 or higher is required for this program"));
135

                
136
        return;
137
    }
138
#endif
139

                
140
    if (!g_thread_supported ()) g_thread_init (NULL);
141
    g_type_init();
142

                
143
#ifdef DARWIN
144
    dirname = g_get_current_dir();
145
    dirname2 = g_build_path(G_DIR_SEPARATOR_S, dirname, "../share/locale", NULL);
146
    bindtextdomain (GETTEXT_PACKAGE, dirname2);
147
    g_free(dirname2);
148
    g_free(dirname);
149
#elif defined(WIN32)
150
    dirname = g_win32_get_package_installation_directory_of_module(NULL);
151
    dirname2 = g_build_path(G_DIR_SEPARATOR_S, dirname, "\\share\\locale", NULL);
152
    bindtextdomain (GETTEXT_PACKAGE, dirname2);
153
    g_free(dirname2);
154
#else
155
    bindtextdomain (GETTEXT_PACKAGE, NNTPGRABLOCALEDIR);
156
#endif
157
    bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
158
    textdomain (GETTEXT_PACKAGE);
159

                
160
#ifdef HAVE_SOUP
161
    /* Due to a bug in libsoup we need to make sure that all GObject's 
162
     * are initialized from the main thread to avoid a deadlock when libsoup
163
     * is called for the first time from a different thread */
164
    SoupSession *session = soup_session_sync_new();
165

                
166
#if 0 
167
    SoupLogger *logger = soup_logger_new(SOUP_LOGGER_LOG_BODY, -1);
168
    soup_session_add_feature(session, SOUP_SESSION_FEATURE(logger));
169
    g_object_unref(logger);
170
#endif
171

                
172
#ifdef HAVE_SOUP_GNOME
173
    /* Enable proxy support when using GNOME 2.26.0 or higher */
174
    soup_session_add_feature_by_type(session, SOUP_TYPE_GNOME_FEATURES_2_26);
175
#endif
176

                
177
#ifdef HAVE_SOUP_GZIP
178
    /* Enable gzip compression */
179
    soup_session_add_feature_by_type(session, SOUP_TYPE_CONTENT_DECODER);
180
#endif
181

                
182
    g_object_unref(session);
183
#endif
184

                
185
#if defined(WIN32) && GLIB_CHECK_VERSION(2,27,1)
186
    /* As of GLib 2.27.1 the return value of the function g_get_user_data_dir() 
187
     * was changed to return CSIDL_LOCAL_APPDATA instead of CSIDL_APPDATA on Win32.
188
     * To prevent a broken NNTPGrab configuration, check if there are configuration
189
     * files at the old path and move them to the new location if necessary */
190
    move_win32_configuration_files_if_necessary();
191
#endif
192
}