Implemented crossplatform automatic startup management.
[udpgate.git] / src / startup.c
index 16114fd..d8e1928 100644 (file)
 #include <glib/gmessages.h>
 
 #include "startup.h"   /* self */
+#include "startup-lsb.h"
 #include "startup-chkconfig.h"
+#include "startup-debian.h"
 
 
-/* FIXME: Provide some layer for multiple 'startup' handling backends. */
+gboolean udpgate_startup_query(UdpgateStartup *udpgate_startup,gboolean *is_on)
+{
+       g_return_val_if_fail(UDPGATE_IS_STARTUP(udpgate_startup),FALSE);
 
+       return (*UDPGATE_STARTUP_GET_CLASS(udpgate_startup)->query)(udpgate_startup,is_on);
+}
 
-gboolean startup_init(void)
+gboolean udpgate_startup_on(UdpgateStartup *udpgate_startup)
 {
-       return startup_chkconfig_init();
+gboolean is_on;
+
+       g_return_val_if_fail(UDPGATE_IS_STARTUP(udpgate_startup),FALSE);
+
+       if (!udpgate_startup_query(udpgate_startup,&is_on))
+               return FALSE;
+       if (is_on==TRUE)
+               return TRUE;
+
+       return (*UDPGATE_STARTUP_GET_CLASS(udpgate_startup)->on)(udpgate_startup);
 }
 
-gboolean startup_query(gboolean *is_on)
+gboolean udpgate_startup_off(UdpgateStartup *udpgate_startup)
 {
-       g_return_val_if_fail(is_on!=NULL,FALSE);
+gboolean is_on;
+
+       g_return_val_if_fail(UDPGATE_IS_STARTUP(udpgate_startup),FALSE);
 
-       return startup_chkconfig_query(is_on);
+       if (!udpgate_startup_query(udpgate_startup,&is_on))
+               return FALSE;
+       if (is_on==FALSE)
+               return TRUE;
+
+       return (*UDPGATE_STARTUP_GET_CLASS(udpgate_startup)->off)(udpgate_startup);
 }
 
-gboolean startup_on(void)
+UdpgateStartup *udpgate_startup_new(void)
+{
+       const GType impl_type_array[]={
+               UDPGATE_TYPE_STARTUP_LSB,
+               UDPGATE_TYPE_STARTUP_CHKCONFIG,
+               UDPGATE_TYPE_STARTUP_DEBIAN,
+               };
+       const GType *impl_type_pointer;
+
+       for (impl_type_pointer = impl_type_array;
+            impl_type_pointer < impl_type_array + G_N_ELEMENTS(impl_type_array);
+            impl_type_pointer++) {
+               UdpgateStartup *udpgate_startup;
+               
+               udpgate_startup=g_object_new(*impl_type_pointer,NULL);
+               g_assert(UDPGATE_IS_STARTUP(udpgate_startup));
+               if (UDPGATE_STARTUP_GET_CLASS(udpgate_startup)->init(udpgate_startup))
+                       return udpgate_startup;
+               g_object_unref(udpgate_startup);
+       }
+
+       return NULL;
+}
+
+static void
+udpgate_startup_class_init (UdpgateStartupClass *class)
 {
-       return startup_chkconfig_on();
 }
 
-gboolean startup_off(void)
+GType
+udpgate_startup_get_type(void)
 {
-       return startup_chkconfig_off();
+  static GType startup_type=0;
+
+  if (!startup_type) {
+               static const GTypeInfo startup_info={
+                       sizeof(UdpgateStartupClass),
+                       NULL,           /* base_init */
+                       NULL,           /* base_finalize */
+                       (GClassInitFunc)udpgate_startup_class_init,
+                       NULL,           /* class_finalize */
+                       NULL,           /* class_data */
+                       sizeof(UdpgateStartup),
+                       0,              /* n_preallocs */
+                       NULL,           /* instance_init */
+                       NULL,           /* value_table */
+      };
+
+               startup_type=g_type_register_static(G_TYPE_OBJECT,"UdpgateStartup",
+                                &startup_info,G_TYPE_FLAG_ABSTRACT);
+    }
+
+  return startup_type;
 }