17
17
// along with this program. If not, see <http://www.gnu.org/licenses/>.
21
using System.Reflection;
24
using org.freedesktop.DBus;
26
using Do.Platform.ServiceStack;
27
using Do.Platform.Windows.DBus;
21
using System.Diagnostics;
22
using System.Windows.Forms;
24
using Do.Platform.ServiceStack;
31
28
namespace Do.Platform.Windows
34
public class SystemService : AbstractSystemService, IController, IInitializedService
31
public class SystemService : AbstractSystemService, IInitializedService
36
33
delegate void BoolDelegate (bool val);
38
const string PowerManagementName = "org.freedesktop.PowerManagement";
39
const string PowerManagementPath = "/org/freedesktop/PowerManagement";
40
35
const string AutoStartKey = "Hidden";
42
[Interface(PowerManagementName)]
43
interface IPowerManagement
46
event BoolDelegate OnBatteryChanged;
49
IPowerManagement power;
50
37
DesktopItem autostartfile;
52
39
public void Initialize ()
41
// not sure if there are any battery state related events
42
// the only ones I could find were for windows mobile & windows CE
56
48
if (Bus.Session.NameHasOwner (PowerManagementName)) {
71
64
public override bool GetOnBatteryPower ()
74
if (power == null && !Bus.Session.NameHasOwner (PowerManagementName))
77
power = Bus.Session.GetObject<IPowerManagement> (PowerManagementName, new ObjectPath (PowerManagementPath));
78
return power.GetOnBattery ();
67
return SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Offline;
79
68
} catch (Exception e) {
80
69
Log<SystemService>.Error ("Could not GetOnBattery: {0}", e.Message);
81
70
Log<SystemService>.Debug (e.StackTrace);
86
75
public override void EnsureSingleApplicationInstance ()
89
IController controller = Registrar.GetControllerInstance ();
90
if (controller == null) {
91
// No IController found on the bus, so no other
92
// instance is running. Register self.
79
if (Process.GetProcessesByName (Process.GetCurrentProcess ().ProcessName).Length > 1) {
93
80
Log<SystemService>.Debug ("No other application instance detected. Continue startup.");
94
Registrar.RegisterController (this);
96
// Another IController was found, so summon it
98
Log<SystemService>.Debug ("Existing application instance detected. Summon and bail.");
82
Log<SystemService>.Debug ("Existing application instance detected. Summon and bail.");
100
84
System.Environment.Exit (0);
102
86
} catch (Exception e) {