GModule disabled moved out to the static building package.
[udpgate.git] / src / startup.c
1 /* $Id$
2  * UDP Gateway utility startup scripts support
3  * Copyright (C) 2004 Jan Kratochvil <project-udpgate@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
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
20 #include "config.h"
21
22 #include <glib/gmessages.h>
23
24 #include "startup.h"    /* self */
25 #include "startup-lsb.h"
26 #include "startup-chkconfig.h"
27 #include "startup-debian.h"
28
29
30 gboolean udpgate_startup_query(UdpgateStartup *udpgate_startup,gboolean *is_on)
31 {
32         g_return_val_if_fail(UDPGATE_IS_STARTUP(udpgate_startup),FALSE);
33
34         return (*UDPGATE_STARTUP_GET_CLASS(udpgate_startup)->query)(udpgate_startup,is_on);
35 }
36
37 gboolean udpgate_startup_on(UdpgateStartup *udpgate_startup)
38 {
39 gboolean is_on;
40
41         g_return_val_if_fail(UDPGATE_IS_STARTUP(udpgate_startup),FALSE);
42
43         if (!udpgate_startup_query(udpgate_startup,&is_on))
44                 return FALSE;
45         if (is_on==TRUE)
46                 return TRUE;
47
48         if (!(*UDPGATE_STARTUP_GET_CLASS(udpgate_startup)->on)(udpgate_startup))
49                 return FALSE;
50         if (!udpgate_startup_query(udpgate_startup,&is_on))
51                 return FALSE;
52         if (is_on!=TRUE) {
53                 g_warning(_("Error registering automatic program startup!"));
54                 return FALSE;
55                 }
56         return TRUE;
57 }
58
59 gboolean udpgate_startup_off(UdpgateStartup *udpgate_startup)
60 {
61 gboolean is_on;
62
63         g_return_val_if_fail(UDPGATE_IS_STARTUP(udpgate_startup),FALSE);
64
65         if (!udpgate_startup_query(udpgate_startup,&is_on))
66                 return FALSE;
67         if (is_on==FALSE)
68                 return TRUE;
69
70         if (!(*UDPGATE_STARTUP_GET_CLASS(udpgate_startup)->off)(udpgate_startup))
71                 return FALSE;
72         if (!udpgate_startup_query(udpgate_startup,&is_on))
73                 return FALSE;
74         if (is_on!=FALSE) {
75                 g_warning(_("Error removing program's system startup registrance!"));
76                 return FALSE;
77                 }
78         return TRUE;
79 }
80
81 UdpgateStartup *udpgate_startup_new(void)
82 {
83         const GType impl_type_array[]={
84                 UDPGATE_TYPE_STARTUP_LSB,
85                 UDPGATE_TYPE_STARTUP_CHKCONFIG,
86                 UDPGATE_TYPE_STARTUP_DEBIAN,
87                 };
88         const GType *impl_type_pointer;
89
90         for (impl_type_pointer = impl_type_array;
91              impl_type_pointer < impl_type_array + G_N_ELEMENTS(impl_type_array);
92              impl_type_pointer++) {
93                 UdpgateStartup *udpgate_startup;
94                 
95                 udpgate_startup=g_object_new(*impl_type_pointer,NULL);
96                 g_assert(UDPGATE_IS_STARTUP(udpgate_startup));
97                 if (UDPGATE_STARTUP_GET_CLASS(udpgate_startup)->init(udpgate_startup))
98                         return udpgate_startup;
99                 g_object_unref(udpgate_startup);
100         }
101
102         return NULL;
103 }
104
105 static void
106 udpgate_startup_class_init (UdpgateStartupClass *class)
107 {
108 }
109
110 GType
111 udpgate_startup_get_type(void)
112 {
113   static GType startup_type=0;
114
115   if (!startup_type) {
116                 static const GTypeInfo startup_info={
117                         sizeof(UdpgateStartupClass),
118                         NULL,           /* base_init */
119                         NULL,           /* base_finalize */
120                         (GClassInitFunc)udpgate_startup_class_init,
121                         NULL,           /* class_finalize */
122                         NULL,           /* class_data */
123                         sizeof(UdpgateStartup),
124                         0,              /* n_preallocs */
125                         NULL,           /* instance_init */
126                         NULL,           /* value_table */
127       };
128
129                 startup_type=g_type_register_static(G_TYPE_OBJECT,"UdpgateStartup",
130                                  &startup_info,G_TYPE_FLAG_ABSTRACT);
131     }
132
133   return startup_type;
134 }