[gnome-dvb-daemon] Allow to add fake devices to settings.ini



commit dc83e8b81ef1c9d355296e8aa061690c5fa86fd3
Author: Sebastian PÃlsterl <sebp k-d-w org>
Date:   Sat Aug 27 13:35:08 2011 +0200

    Allow to add fake devices to settings.ini
    
    Example of a fake DVB-T device in settings.ini.
    Keep in mind that channels.conf must contain
    valid tuning data for the specified adapter type.
    
    [device.1]
    name=Something
    type=DVB-T
    adapter=1
    frontend=0
    channels_file=/home/sebp/channels.conf
    recordings_dir=/tmp

 src/Device.vala   |   32 ++++++++++++++++++++++++++------
 src/Main.vala     |   21 +++++++++++++++++++++
 src/Manager.vala  |   18 ++++++++++--------
 src/Settings.vala |   47 +++++++++++++++++++++++++++++++++++++++++++----
 4 files changed, 100 insertions(+), 18 deletions(-)
---
diff --git a/src/Device.vala b/src/Device.vala
index 71916e7..6dd04bf 100644
--- a/src/Device.vala
+++ b/src/Device.vala
@@ -58,6 +58,17 @@ namespace DVB {
             base (Adapter: adapter, Frontend: frontend);
         }
 
+        public static Device new_set_type (uint adapter, uint frontend,
+            File channels_conf, File recordings_dir, string name, AdapterType type) {
+            Device dev = new Device (adapter, frontend);
+            dev.adapter_name = name;
+            dev.adapter_type = type;
+            dev.RecordingsDirectory = recordings_dir;
+            dev.Channels = new ChannelList (channels_conf);
+
+            return dev;
+        }
+
         public static Device new_with_type (uint adapter, uint frontend) {
         	var device = new Device (adapter, frontend);
             
@@ -180,13 +191,22 @@ namespace DVB {
                
             pipeline.set_state(State.NULL);
 
-            if (adapter_type == "DVB-T") this.adapter_type = AdapterType.DVB_T;
-            else if (adapter_type == "DVB-S") this.adapter_type = AdapterType.DVB_S;
-            else if (adapter_type == "DVB-C") this.adapter_type = AdapterType.DVB_C;
-            else this.adapter_type = AdapterType.UNKNOWN;
-            
+            this.adapter_type = get_type_from_string (adapter_type);
+
             return success;
         }
+
+        public static AdapterType get_type_from_string (string? adapter_type) {
+            if (adapter_type == null)
+                return AdapterType.UNKNOWN;
+
+            AdapterType type;
+            if (adapter_type == "DVB-T") type = AdapterType.DVB_T;
+            else if (adapter_type == "DVB-S") type = AdapterType.DVB_S;
+            else if (adapter_type == "DVB-C") type = AdapterType.DVB_C;
+            else type = AdapterType.UNKNOWN;
+            return type;
+        }
     }
-    
+
 }
diff --git a/src/Main.vala b/src/Main.vala
index 2583b07..37767aa 100644
--- a/src/Main.vala
+++ b/src/Main.vala
@@ -125,9 +125,30 @@ namespace Main {
             return;
         }
 
+        uint max_group_id = 0;
         log.info ("Restoring %d device groups", device_groups.size);
         foreach (DVB.DeviceGroup device_group in device_groups) {
             manager.restore_device_group_and_timers (device_group);
+            if (device_group.Id > max_group_id)
+                max_group_id = device_group.Id;
+        }
+
+        restore_fake_devices (max_group_id);
+    }
+
+    private static void restore_fake_devices (uint max_group_id) {
+        DVB.Settings settings = DVB.Factory.get_settings ();
+        Gee.List<DVB.Device> devices = settings.get_fake_devices ();
+        if (devices.size > 0) {
+            DVB.Device ref_dev = devices.get (0);
+            DVB.DeviceGroup group = new DVB.DeviceGroup (max_group_id + 1,
+                ref_dev, false);
+            group.Name = "Fake Devices";
+            for (int i=1; i<devices.size; i++) {
+                group.add (devices.get (i));
+            }
+
+            manager.restore_device_group (group, false);
         }
     }
 
diff --git a/src/Manager.vala b/src/Manager.vala
index f1e42fb..888621d 100644
--- a/src/Manager.vala
+++ b/src/Manager.vala
@@ -385,7 +385,7 @@ namespace DVB {
          *
          * Register device, create Recorder and ChannelList D-Bus service
          */
-        public bool add_device_group (DeviceGroup devgroup) {
+        public bool add_device_group (DeviceGroup devgroup, bool store) {
             uint group_id = devgroup.Id;
             log.debug ("Adding device group %u with %d devices", group_id,
                 devgroup.size);
@@ -399,11 +399,13 @@ namespace DVB {
             lock (this.devices) {
                 this.devices.set (group_id, devgroup);
             }
-            try {
-                Factory.get_config_store ().add_device_group (devgroup);
-            } catch (SqlError e) {
-                log.error ("%s", e.message);
-                return false;
+            if (store) {
+                try {
+                    Factory.get_config_store ().add_device_group (devgroup);
+                } catch (SqlError e) {
+                    log.error ("%s", e.message);
+                    return false;
+                }
             }
             devgroup.device_removed.connect (this.on_device_removed_from_group);
 
@@ -419,7 +421,7 @@ namespace DVB {
             return true;
         }
 
-        public bool restore_device_group (DeviceGroup device_group) {
+        public bool restore_device_group (DeviceGroup device_group, bool store = true) {
             log.debug ("Restoring group %u", device_group.Id);
 
             try {
@@ -429,7 +431,7 @@ namespace DVB {
             	return false;
             }
 
-            return this.add_device_group (device_group);
+            return this.add_device_group (device_group, store);
         }
 
         public void restore_timers (DeviceGroup device_group) {
diff --git a/src/Settings.vala b/src/Settings.vala
index 4708a3c..5031da5 100644
--- a/src/Settings.vala
+++ b/src/Settings.vala
@@ -36,6 +36,14 @@ namespace DVB {
         private static const string STREAMING_SECTION = "streaming";
         private static const string INTERFACE = "interface";
 
+        private static const string DEVICE_SECTION_PREFIX = "device.";
+        private static const string DEVICE_NAME = "name";
+        private static const string DEVICE_TYPE = "type";
+        private static const string DEVICE_ADAPTER = "adapter";
+        private static const string DEVICE_FRONTEND = "frontend";
+        private static const string DEVICE_CHANNELS_FILE = "channels_file";
+        private static const string DEVICE_RECORDINGS_DIR = "recordings_dir";
+
         private static const int DEFAULT_MARGIN_START = 5;
         private static const int DEFAULT_MARGIN_END = 5;
         private static const int DEFAULT_SCAN_INTERVAL = 30;
@@ -61,7 +69,7 @@ namespace DVB {
             try {
                 val = this.get_integer (EPG_SECTION, SCAN_INTERVAL);
             } catch (KeyFileError e) {
-                warning ("%s", e.message);
+                log.warning ("%s", e.message);
                 val = DEFAULT_SCAN_INTERVAL;
             }
             return val * 60;
@@ -72,7 +80,7 @@ namespace DVB {
             try {
                 start_margin = this.get_integer (TIMERS_SECTION, MARGIN_START);
             } catch (KeyFileError e) {
-                warning ("%s", e.message);
+                log.warning ("%s", e.message);
                 start_margin = DEFAULT_MARGIN_START;
             }
             return start_margin;
@@ -83,7 +91,7 @@ namespace DVB {
             try {
                 end_margin = this.get_integer (TIMERS_SECTION, MARGIN_END);
             } catch (KeyFileError e) {
-                warning ("%s", e.message);
+                log.warning ("%s", e.message);
                 end_margin = DEFAULT_MARGIN_END;
             }
             return end_margin;
@@ -94,12 +102,43 @@ namespace DVB {
             try {
                 val = this.get_string (STREAMING_SECTION, INTERFACE);
             } catch (KeyFileError e) {
-                warning ("%s", e.message);
+                log.warning ("%s", e.message);
                 val = DEFAULT_INTERFACE;
             }
             return val;
         }
 
+        public Gee.List<Device> get_fake_devices () {
+            Gee.List<Device> devices = new Gee.ArrayList<Device> ();
+            string[] groups = this.keyfile.get_groups ();
+            foreach (string group in groups) {
+                if (group.has_prefix (DEVICE_SECTION_PREFIX)) {
+                    try {
+                        Device dev = this.get_device (group);
+                        devices.add (dev);
+                    } catch (KeyFileError e) {
+                        log.warning ("%s", e.message);
+                    }
+                }
+            }
+            return devices;
+        }
+
+        private Device get_device (string group) throws KeyFileError {
+            string name = this.get_string (group, DEVICE_NAME);
+            int adapter = this.get_integer (group, DEVICE_ADAPTER);
+            int frontend = this.get_integer (group, DEVICE_FRONTEND);
+
+            string typestr = this.get_string (group, DEVICE_TYPE);
+            AdapterType type = Device.get_type_from_string (typestr);
+
+            File channels = File.new_for_path (this.get_string (group, DEVICE_CHANNELS_FILE));
+            File rec_dir = File.new_for_path (this.get_string (group, DEVICE_RECORDINGS_DIR));
+
+            return Device.new_set_type (adapter, frontend, channels, rec_dir,
+                name, type);
+        }
+
         public File get_settings_file () {
             File config_dir = File.new_for_path (
                 Environment.get_user_config_dir ());



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]