aboutsummaryrefslogtreecommitdiffstats
path: root/modules/windows-sens/evolution-windows-sens.cxx
blob: 0748b64c6d6a79c8a457583c4cb24d227c38f188 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * evolution-windows-sens.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 */

#ifdef __cplusplus
extern "C" {
#endif

#define INITGUID
#include <windows.h>
#include <eventsys.h>
#include <sensevts.h>
#include <stdio.h>


#include <shell/e-shell.h>
#include <e-util/e-extension.h>

#define NUM_ELEMENTS(x) (sizeof((x)) / sizeof((x)[0]))

/* Standard GObject macros */
#define E_TYPE_WINDOWS_SENS \
    (e_windows_sens_get_type ())
#define E_WINDOWS_SENS(obj) \
    (G_TYPE_CHECK_INSTANCE_CAST \
    ((obj), E_TYPE_WINDOWS_SENS, EWindowsSENS))

typedef struct _EWindowsSENS EWindowsSENS;
typedef struct _EWindowsSENSClass EWindowsSENSClass;

struct _EWindowsSENS {
    EExtension parent;
};

struct _EWindowsSENSClass {
    EExtensionClass parent_class;
};

/* Module Entry Points */
void e_module_load (GTypeModule *type_module);
void e_module_unload (GTypeModule *type_module);

/* Forward Declarations */
GType e_windows_sens_get_type (void);

G_DEFINE_DYNAMIC_TYPE (EWindowsSENS, e_windows_sens, E_TYPE_EXTENSION)

static EShell *
windows_sens_get_shell (EWindowsSENS *extension)
{
    EExtensible *extensible;

    extensible = e_extension_get_extensible (E_EXTENSION (extension));

    return E_SHELL (extensible);
}

class MySensNetwork : public ISensNetwork
{
private:
    long ref;
    EWindowsSENS *mpEWS;

public:
    MySensNetwork (EWindowsSENS *ews) :
        ref(1),
        mpEWS(ews)
    {}

    HRESULT WINAPI QueryInterface (REFIID iid, void ** ppv)
    {
        if (IsEqualIID (iid, IID_IUnknown) || IsEqualIID (iid, IID_IDispatch) || IsEqualIID (iid, IID_ISensNetwork)) {
            *ppv = this;
            AddRef();
            return S_OK;
        }
        *ppv = NULL;
        return E_NOINTERFACE;
    }

    ULONG WINAPI AddRef ()
    {
        return InterlockedIncrement (&ref);
    }

    ULONG WINAPI Release ()
    {
        int tmp = InterlockedDecrement (&ref);
        return tmp;
    }

    HRESULT WINAPI GetTypeInfoCount (unsigned FAR*)
    {
        return E_NOTIMPL;
    }

    HRESULT WINAPI GetTypeInfo (unsigned, LCID, ITypeInfo FAR* FAR*)
    {
        return E_NOTIMPL;
    }

    HRESULT WINAPI GetIDsOfNames (REFIID, OLECHAR FAR* FAR*, unsigned, LCID, DISPID FAR*)
    {
        return E_NOTIMPL;
    }

    HRESULT WINAPI Invoke (DISPID, REFIID, LCID, WORD, DISPPARAMS FAR*, VARIANT FAR*, EXCEPINFO FAR*, unsigned FAR*)
    {
        return E_NOTIMPL;
    }

// ISensNetwork methods:
    virtual HRESULT WINAPI ConnectionMade (BSTR, ULONG ulType, LPSENS_QOCINFO)
    {
        if (ulType) {
            EShell *shell = windows_sens_get_shell (mpEWS);
            e_shell_set_network_available (shell, TRUE);
        }
        return S_OK;
    }

    virtual HRESULT WINAPI ConnectionMadeNoQOCInfo (BSTR, ULONG)
    {
        //Always followed by ConnectionMade
        return S_OK;
    }

    virtual HRESULT WINAPI ConnectionLost (BSTR, ULONG ulType)
    {
        if (ulType) {
            EShell *shell = windows_sens_get_shell (mpEWS);
            e_shell_set_network_available (shell, FALSE);
        }
        return S_OK;
    }

    virtual HRESULT WINAPI DestinationReachable(BSTR, BSTR , ULONG ulType, LPSENS_QOCINFO)
    {
        if (ulType) {
            EShell *shell = windows_sens_get_shell (mpEWS);
            e_shell_set_network_available (shell, TRUE);
        }
        return S_OK;
    }

    virtual HRESULT WINAPI DestinationReachableNoQOCInfo(BSTR bstrDestination,BSTR bstrConnection,ULONG ulType)
    {
        return S_OK;
    }
};

/* 4E14FB9F-2E22-11D1-9964-00C04FBBB345 */
DEFINE_GUID(IID_IEventSystem, 0x4E14FB9F, 0x2E22, 0x11D1, 0x99, 0x64, 0x00, 0xC0, 0x4F, 0xBB, 0xB3, 0x45);

/* 4A6B0E15-2E38-11D1-9965-00C04FBBB345 */
DEFINE_GUID(IID_IEventSubscription, 0x4A6B0E15, 0x2E38, 0x11D1, 0x99, 0x65, 0x00, 0xC0, 0x4F, 0xBB, 0xB3, 0x45);

/* d597bab1-5b9f-11d1-8dd2-00aa004abd5e */
DEFINE_GUID(IID_ISensNetwork, 0xd597bab1, 0x5b9f, 0x11d1, 0x8d, 0xd2, 0x00, 0xaa, 0x00, 0x4a, 0xbd, 0x5e);

/* 4E14FBA2-2E22-11D1-9964-00C04FBBB345 */
DEFINE_GUID(CLSID_CEventSystem, 0x4E14FBA2, 0x2E22, 0x11D1, 0x99, 0x64, 0x00, 0xC0, 0x4F, 0xBB, 0xB3, 0x45);

/* 7542e960-79c7-11d1-88f9-0080c7d771bf */
DEFINE_GUID(CLSID_CEventSubscription, 0x7542e960, 0x79c7, 0x11d1, 0x88, 0xf9, 0x00, 0x80, 0xc7, 0xd7, 0x71, 0xbf);

static void
windows_sens_constructed (GObject *object)
{
    static IEventSystem *pIEventSystem =0;
    static IEventSubscription* pIEventSubscription = 0;
    WCHAR buffer[64];
    static const char* eventclassid="{D5978620-5B9F-11D1-8DD2-00AA004ABD5E}";
    static const char* methods[]={"ConnectionMade","ConnectionMadeNoQOCInfo","ConnectionLost","DestinationReachable","DestinationReachableNoQOCInfo"};
    static const char* names[]={"EWS_ConnectionMade","EWS_ConnectionMadeNoQOCInfo","EWS_ConnectionLost","EWS_DestinationReachable","EWS_DestinationReachableNoQOCInfo"};
    static const char* subids[]={"{cd1dcbd6-a14d-4823-a0d2-8473afde360f}","{a82f0e80-1305-400c-ba56-375ae04264a1}","{45233130-b6c3-44fb-a6af-487c47cee611}",
                                 "{51377df7-1d29-49eb-af32-4fff77b059fb}","{d16830d3-7a3a-4240-994b-a1fa344385dd}"};

    EWindowsSENS *extension = (E_WINDOWS_SENS (object));
    static MySensNetwork *pISensNetwork = new MySensNetwork (extension);

    CoInitialize(0);

    HRESULT res=CoCreateInstance (CLSID_CEventSystem, 0,CLSCTX_SERVER,IID_IEventSystem,(void**)&pIEventSystem);

    for (unsigned i=0; i<NUM_ELEMENTS(methods); i++)
    {
        res=CoCreateInstance (CLSID_CEventSubscription, 0, CLSCTX_SERVER, IID_IEventSubscription, (LPVOID*)&pIEventSubscription);
        MultiByteToWideChar (0, 0, eventclassid, -1, buffer, 64);
        res=pIEventSubscription->put_EventClassID (buffer);
        res=pIEventSubscription->put_SubscriberInterface ((IUnknown*)pISensNetwork);
        MultiByteToWideChar (0, 0, methods[i], -1, buffer, 64);
        res=pIEventSubscription->put_MethodName (buffer);
        MultiByteToWideChar (0, 0, names[i], -1, buffer, 64);
        res=pIEventSubscription->put_SubscriptionName (buffer);
        MultiByteToWideChar (0, 0, subids[i], -1, buffer, 64);
        res=pIEventSubscription->put_SubscriptionID (buffer);

        /* Make the subscription receive the event only if the ownerof the subscription
         * is logged on to the same computer as the publisher. This makes this module
         * work on Windows Vista and Windows 7 with normal user account.
         */
        res=pIEventSubscription->put_PerUser(TRUE);

        res=pIEventSystem->Store (PROGID_EventSubscription, (IUnknown*)pIEventSubscription);
        pIEventSubscription->Release ();
        pIEventSubscription=0;
    }

    typedef BOOL (WINAPI* IsNetworkAlive_t) (LPDWORD);

    IsNetworkAlive_t pIsNetworkAlive = NULL;

    HMODULE hDLL=LoadLibrary ("sensapi.dll");

    BOOL alive = TRUE;
    if ((pIsNetworkAlive=(IsNetworkAlive_t) GetProcAddress (hDLL, "IsNetworkAlive"))) {
        DWORD Network;
        alive=pIsNetworkAlive (&Network);
    }

    FreeLibrary(hDLL);

    EShell *shell = windows_sens_get_shell (extension);

    e_shell_set_network_available (shell, alive);
}

static void
e_windows_sens_class_init (EWindowsSENSClass *_class)
{
    GObjectClass *object_class;
    EExtensionClass *extension_class;

    object_class = G_OBJECT_CLASS (_class);
    object_class->constructed = windows_sens_constructed;

    extension_class = E_EXTENSION_CLASS (_class);
    extension_class->extensible_type = E_TYPE_SHELL;
}

static void
e_windows_sens_class_finalize (EWindowsSENSClass *_class)
{
    CoUninitialize();
}

static void
e_windows_sens_init (EWindowsSENS *extension)
{
}

G_MODULE_EXPORT void
e_module_load (GTypeModule *type_module)
{
    e_windows_sens_register_type (type_module);
}

G_MODULE_EXPORT void
e_module_unload (GTypeModule *type_module)
{
}

#ifdef __cplusplus
}
#endif