~cairo-dock-team/cairo-dock-plug-ins/plug-ins

« back to all changes in this revision

Viewing changes to Indicator-Generic/src/applet-launcher.c

  • Committer: Matthieu Baerts
  • Date: 2014-10-19 00:26:10 UTC
  • Revision ID: matttbe@gmail.com-20141019002610-ulf26s9b4c4rw10r
We just switched from BZR to Git.
Follow us on Github: https://github.com/Cairo-Dock

Note: we will only use Github to manage our source code and all pull requests.
Please continue to report your bugs/ideas/messages on our forum or Launchpad! 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
* This file is a part of the Cairo-Dock project
3
 
*
4
 
* Copyright : (C) see the 'copyright' file.
5
 
* E-mail    : see the 'copyright' file.
6
 
*
7
 
* This program is free software; you can redistribute it and/or
8
 
* modify it under the terms of the GNU General Public License
9
 
* as published by the Free Software Foundation; either version 3
10
 
* of the License, or (at your option) any later version.
11
 
*
12
 
* This program is distributed in the hope that it will be useful,
13
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
* GNU General Public License for more details.
16
 
* You should have received a copy of the GNU General Public License
17
 
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
*/
19
 
 
20
 
 
21
 
#include "applet-launcher.h"
22
 
#include "applet-struct.h"
23
 
 
24
 
// if it's not in the exception list
25
 
static gboolean _is_an_exception (const gchar *cIndicatorName, gchar **cExceptionsList)
26
 
{
27
 
        if (cExceptionsList != NULL)
28
 
        {
29
 
                for (int i = 0; cExceptionsList[i] != NULL; i++)
30
 
                {
31
 
                        if (g_strcmp0 (cIndicatorName, cExceptionsList[i]) == 0)
32
 
                                return TRUE;
33
 
                }
34
 
        }
35
 
        return FALSE;
36
 
}
37
 
 
38
 
static void _on_file_event (CairoDockFMEventType iEventType, const gchar *cURI, GldiModuleInstance *myApplet)
39
 
{
40
 
        g_return_if_fail (cURI != NULL);
41
 
        CD_APPLET_ENTER;
42
 
 
43
 
        /* No need to reload all indicators if one is modified:
44
 
         *  these files are modified but the corresponding daemons are not reloaded
45
 
         */
46
 
        if (iEventType != CAIRO_DOCK_FILE_MODIFIED) // created/removed
47
 
        {
48
 
                cd_debug ("File event: Reload all indicators");
49
 
                cd_indicator_generic_reload_all_indicators (myApplet);
50
 
        }
51
 
 
52
 
        CD_APPLET_LEAVE();
53
 
}
54
 
 
55
 
void cd_indicator_generic_add_monitor_dir (GldiModuleInstance *myApplet)
56
 
{
57
 
        cairo_dock_fm_add_monitor_full (cd_indicator3_get_directory_path (), TRUE,
58
 
                NULL, (CairoDockFMMonitorCallback) _on_file_event, myApplet);
59
 
        #ifdef IS_INDICATOR_NG
60
 
        cairo_dock_fm_add_monitor_full (INDICATOR_SERVICE_DIR, TRUE,
61
 
                NULL, (CairoDockFMMonitorCallback) _on_file_event, myApplet);
62
 
        #endif
63
 
}
64
 
 
65
 
void cd_indicator_generic_remove_monitor_dir (void)
66
 
{
67
 
        cairo_dock_fm_remove_monitor_full (cd_indicator3_get_directory_path (),
68
 
                TRUE, NULL);
69
 
        #ifdef IS_INDICATOR_NG
70
 
        cairo_dock_fm_remove_monitor_full (INDICATOR_SERVICE_DIR, TRUE, NULL);
71
 
        #endif
72
 
}
73
 
 
74
 
GDir * cd_indicator_generic_open_dir_modules (GldiModuleInstance *myApplet)
75
 
{
76
 
        GError *error = NULL;
77
 
        GDir *pDir = g_dir_open (cd_indicator3_get_directory_path (), 0, &error); // all indicators are on the same dir
78
 
        if (error != NULL)
79
 
        {
80
 
                cd_warning ("Failed to load indicator3 modules dir: %s", cd_indicator3_get_directory_path ());
81
 
                return NULL;
82
 
        }
83
 
        return pDir;
84
 
}
85
 
 
86
 
GDir * cd_indicator_generic_open_dir_sevices (GldiModuleInstance *myApplet)
87
 
{
88
 
        #ifdef IS_INDICATOR_NG
89
 
        GError *error = NULL;
90
 
        GDir *pDir = g_dir_open (INDICATOR_SERVICE_DIR, 0, &error); // all indicators are on the same dir
91
 
        if (error != NULL)
92
 
        {
93
 
                cd_warning ("Failed to load indicator3 services dir: %s", INDICATOR_SERVICE_DIR);
94
 
                return NULL;
95
 
        }
96
 
        return pDir;
97
 
        #else
98
 
        return NULL;
99
 
        #endif
100
 
}
101
 
 
102
 
static gint _load_all_indicators_in_dir (GldiModuleInstance *myApplet, GDir *pDir, gboolean bIsModule)
103
 
{
104
 
        // for each indicator file, instanciate a new plugin with it
105
 
        // (useful to place it where we want, all icons are not regrouped into one big icon)
106
 
        const gchar *cFileName;
107
 
        gchar *cInstanceFilePath;
108
 
        GldiModuleInstance *pModuleInstance;
109
 
        gint iNbFiles = 0;
110
 
        while ((cFileName = g_dir_read_name (pDir)) != NULL)
111
 
        {
112
 
                if (*cFileName == '\0' || (bIsModule && ! g_str_has_suffix (cFileName, G_MODULE_SUFFIX))
113
 
                        || _is_an_exception (cFileName, myConfig.cExceptionsList))
114
 
                        continue;
115
 
 
116
 
                gchar *cUserDataDirPath = gldi_module_get_config_dir (myApplet->pModule);
117
 
                // config file: indicator.so.conf ; e.g. libprintersmenu.so.conf
118
 
                cInstanceFilePath = g_strdup_printf ("%s/%s.conf", cUserDataDirPath, cFileName);
119
 
                if (! g_file_test (cInstanceFilePath, G_FILE_TEST_EXISTS))
120
 
                {
121
 
                        // new indicator: create the .conf file
122
 
                        gchar *cConfFileOriginalPath = g_strdup_printf ("%s/%s",
123
 
                                cUserDataDirPath, myApplet->pModule->pVisitCard->cConfFileName);
124
 
 
125
 
                        GKeyFile *pKeyFile = cairo_dock_open_key_file (cConfFileOriginalPath);
126
 
                        g_free (cConfFileOriginalPath);
127
 
                        if (pKeyFile != NULL)
128
 
                        {
129
 
                                // added the indicator in the .conf file
130
 
                                g_key_file_set_string (pKeyFile, "Configuration", "indicator", cFileName);
131
 
                                // remove exceptions' lists
132
 
                                cairo_dock_remove_group_key_from_conf_file (pKeyFile, "Configuration", "except-edit");
133
 
                                g_key_file_remove_key (pKeyFile, "Configuration", "exceptions", NULL);
134
 
                                // write
135
 
                                cairo_dock_write_keys_to_file (pKeyFile, cInstanceFilePath);
136
 
                                // free
137
 
                                g_key_file_free (pKeyFile);
138
 
                        }
139
 
                }
140
 
                // create the new icon
141
 
                pModuleInstance = gldi_module_instance_new (myApplet->pModule, cInstanceFilePath);  // we don't have to free cInstanceFilePath
142
 
                myData.pIndicatorsList = g_list_prepend (myData.pIndicatorsList, pModuleInstance);
143
 
                g_free (cUserDataDirPath);
144
 
                iNbFiles++;
145
 
        }
146
 
        g_dir_close (pDir);
147
 
        return iNbFiles;
148
 
}
149
 
 
150
 
gint cd_indicator_generic_load_all_indicators (GldiModuleInstance *myApplet, GDir *pDirModules, GDir *pDirServices)
151
 
{
152
 
        gint iNbFiles = 0;
153
 
        if (pDirModules != NULL)
154
 
                iNbFiles = _load_all_indicators_in_dir (myApplet, pDirModules, TRUE);
155
 
        if (pDirServices != NULL)
156
 
                iNbFiles += _load_all_indicators_in_dir (myApplet, pDirServices, FALSE);
157
 
        return iNbFiles;
158
 
}
159
 
 
160
 
void cd_indicator_generic_reload_all_indicators (GldiModuleInstance *myApplet)
161
 
{
162
 
        cd_debug ("Reload all indicators");
163
 
        g_list_foreach (myData.pIndicatorsList, (GFunc)gldi_object_unref, NULL);
164
 
 
165
 
        g_list_free (myData.pIndicatorsList);
166
 
        myData.pIndicatorsList = NULL;
167
 
 
168
 
        GDir *pDirModules = cd_indicator_generic_open_dir_modules (myApplet);
169
 
        GDir *pDirServices = cd_indicator_generic_open_dir_sevices (myApplet);
170
 
        if (pDirModules == NULL && pDirServices == NULL)
171
 
        {
172
 
                myApplet->pModule->pVisitCard->iContainerType = CAIRO_DOCK_MODULE_IS_PLUGIN; // dir is empty...
173
 
                return;
174
 
        }
175
 
 
176
 
        myApplet->pModule->pVisitCard->iContainerType = CAIRO_DOCK_MODULE_CAN_DOCK | CAIRO_DOCK_MODULE_CAN_DESKLET;
177
 
 
178
 
        if (cd_indicator_generic_load_all_indicators (myApplet, pDirModules, pDirServices) == 0)
179
 
                myApplet->pModule->pVisitCard->iContainerType = CAIRO_DOCK_MODULE_IS_PLUGIN; // dir is empty...
180
 
}