Statistics
| Revision:

root / branches / nntpgrab-0.7 / plugins / networkmanager / networkmanager.c @ 1921

History | View | Annotate | Download (4.7 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
#include 
20
#include 
21
#include "nntpgrab_plugin.h"
22
#include "config.h"
23

                
24
static void nm_state_changed_cb(GObject *gobject, GParamSpec *pspec, gpointer user_data);
25

                
26
struct _nm_plugin_data {
27
    NMClient *client;
28
    gboolean schedular_stopped_by_this_plugin;
29
};
30

                
31
void
32
nntpgrab_plugin_initialize(NGPlugin *plugin_data)
33
{
34
    ng_plugin_set_name(plugin_data, "NetworkManager");
35
    ng_plugin_set_version(plugin_data, PACKAGE_VERSION);
36
    ng_plugin_set_author(plugin_data, "Erik van Pienbroek");
37
    ng_plugin_set_url(plugin_data, "https://www.nntpgrab.nl");
38
    ng_plugin_set_description(plugin_data, "Plugin which offers integration with NetworkManager. Whenever a network connection is lost, the schedular will automatically get paused. When the network connection is resumed, the schedular will automatically be resumed");
39
}
40

                
41
ngboolean
42
nntpgrab_plugin_load(NGPlugin *plugin_data, char **errmsg)
43
{
44
    struct _nm_plugin_data *priv = g_slice_new0(struct _nm_plugin_data);
45
    priv->client = (gpointer) nm_client_new();
46
    priv->schedular_stopped_by_this_plugin = FALSE;
47
    plugin_data->priv = (gpointer) priv;
48

                
49
    g_signal_connect(G_OBJECT(priv->client), "notify::state", G_CALLBACK(nm_state_changed_cb), plugin_data);
50

                
51
    return TRUE;
52
}
53

                
54
ngboolean
55
nntpgrab_plugin_can_unload(NGPlugin *plugin_data, char **reason)
56
{
57
    return TRUE;
58
}
59

                
60
void
61
nntpgrab_plugin_unload(NGPlugin *plugin_data)
62
{
63
    if (plugin_data->priv) {
64
        g_object_unref(((struct _nm_plugin_data*) plugin_data->priv)->client);
65
        g_slice_free(struct _nm_plugin_data, plugin_data->priv);
66

                
67
        plugin_data->priv = NULL;
68
    }
69
}
70

                
71
void
72
nntpgrab_plugin_destroy(NGPlugin *plugin_data)
73
{
74
}
75

                
76
int
77
nntpgrab_plugin_get_version(void)
78
{
79
    return NNTPGRAB_PLUGIN_API_VERSION;
80
}
81

                
82
gboolean
83
start_schedular_cb(gpointer data)
84
{
85
    NGPlugin *plugin_data = (NGPlugin*) data;
86

                
87
    switch (nm_client_get_state(NM_CLIENT(((struct _nm_plugin_data*)plugin_data->priv)->client))) {
88
        case NM_STATE_CONNECTED:
89
        case NM_STATE_UNKNOWN:
90
            if (((struct _nm_plugin_data*)plugin_data->priv)->schedular_stopped_by_this_plugin &&
91
                plugin_data->core_funcs.schedular_get_state() == SCHEDULAR_STATE_STOPPED) {
92

                
93
                if (plugin_data->core_funcs.schedular_start() == FALSE) {
94
                    /* The schedular is probably still being paused, try again later */
95
                    return TRUE;
96
                }
97

                
98
                ((struct _nm_plugin_data*)plugin_data->priv)->schedular_stopped_by_this_plugin = FALSE;
99
            }
100

                
101
            break;
102

                
103
        case NM_STATE_ASLEEP:
104
        case NM_STATE_CONNECTING:
105
        case NM_STATE_DISCONNECTED:
106
            break;
107
    }
108

                
109
    return FALSE;
110
}
111

                
112
static void
113
nm_state_changed_cb(GObject *gobject, GParamSpec *pspec, gpointer user_data)
114
{
115
    NGPlugin *plugin_data = (NGPlugin*) user_data;
116

                
117
    g_print("NetworkManager state changed! new state = %i\n", nm_client_get_state(NM_CLIENT(gobject)));
118
    switch (nm_client_get_state(NM_CLIENT(gobject))) {
119
        case NM_STATE_ASLEEP:
120
        case NM_STATE_DISCONNECTED:
121
            /* Only stop the schedular when it is still running */
122
            if (plugin_data->core_funcs.schedular_get_state() == SCHEDULAR_STATE_RUNNING) {
123
                ((struct _nm_plugin_data*)plugin_data->priv)->schedular_stopped_by_this_plugin = TRUE;
124
                plugin_data->core_funcs.schedular_stop(NULL, FALSE);        /* We don't use the reason field here as we don't want to show the user a popup for this situation */
125
            }
126
            break;
127
        case NM_STATE_CONNECTED:
128
        case NM_STATE_UNKNOWN:
129
            /* Frequently it takes a few additional seconds to get the network fully up and running so we use a timer here */
130
#if GLIB_CHECK_VERSION(2,14,0)
131
            g_timeout_add_seconds(5, start_schedular_cb, plugin_data);
132
#else
133
            g_timeout_add(5000, start_schedular_cb, plugin_data);
134
#endif
135
            break;
136

                
137
        default:
138
        case NM_STATE_CONNECTING:
139
            break;
140
    }
141
}