Merge lp:~gotopad/do-plugins/TimerApplet into lp:do-plugins

Proposed by gotopad
Status: Work in progress
Proposed branch: lp:~gotopad/do-plugins/TimerApplet
Merge into: lp:do-plugins
Diff against target: None lines
To merge this branch: bzr merge lp:~gotopad/do-plugins/TimerApplet
Reviewer Review Type Date Requested Status
Alex Launi (community) Needs Fixing
Review via email: mp+7364@code.launchpad.net
To post a comment you must log in.
Revision history for this message
gotopad (gotopad) wrote :

TimerApplet-Plugin for the TimerApplet for the GNOME panel (http://timerapplet.sourceforge.net/)

Revision history for this message
Alex Launi (alexlauni) wrote :

alphabetize your makefile sections. the files, refs, and resources should be listed in alphabetical order.

You also need to update some stuff for trunk, we no longer have an addin.xml but an addin.xml.in. There's not much difference, you just need to define a GetText localizer, look at another plugin and basically copy and paste.

You've got formatting issues in basically every file, both indentation/spacing as well as no new lines where there should be new lines. If you finish a method, put a newline before you start a new one. Make your code a work of art, not a scribble on the back of a notebook. Make sure you review the mono guidelines. We follow them with the exception of indentation we use 1 tab, and have a loose 120 char rule instead of 80 -> http://www.mono-project.com/Coding_Guidelines

TimerApplet.cs:

first off all interfaces should start with the letter I, so that should be ITimer.

You've hardcoded the ObjectPath to timer_3, this is terrible. That's not the least bit guaranteed. You need to ask "/net/sourceforge/timerapplet/TimerApplet/TimerManager" for a list of applets available, and it will give you an array of applets (like "applet_3"). You then need to use each one.

For your block where you've implemented the methods of the Timer interface, instead of putting a comment above each method, before the first put #region ITimer implementation and at the end put #endregion. This is general .NET style.

in HasOwner, you do a really roundabout way of doing an if/else. Just do an if/else.

Also, you call HasOwner for every one of your ITimer methods, this is going to cause you to get a new timer object every time! Why? Just get it once and cache it. Look at how the other plugins that use dbus do it. Pidgin, and banshee are two examples that come to mind instantly.

in PauseContinueTimer.cs and StopTimer.cs

In SupportsItem, you do an if () and then outside of that you just return false. You can save a level of indentation by doing
if (!item is PresetItem) return false; and then in the same block as SupportsItem do your work. Makes the code much more readable.

in StartTimer.cs

If all SupportsItem does is return true, you don't even need it. This is the default behaviour.

review: Needs Fixing

Unmerged revisions

624. By gotopad

Indentation corrected

623. By gotopad

Added TimerApplet to DoPlugins.mds

622. By gotopad

Added TimerApplet directory. Changed Makefile.am and configure.ac

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'DoPlugins.mds'
--- DoPlugins.mds 2009-06-11 07:36:57 +0000
+++ DoPlugins.mds 2009-06-11 22:04:04 +0000
@@ -73,6 +73,7 @@
73 <Entry build="True" name="Emesene" configuration="Debug" />73 <Entry build="True" name="Emesene" configuration="Debug" />
74 <Entry build="True" name="Dropbox" configuration="Debug" />74 <Entry build="True" name="Dropbox" configuration="Debug" />
75 <Entry build="True" name="RemindMe" configuration="Debug" />75 <Entry build="True" name="RemindMe" configuration="Debug" />
76 <Entry build="True" name="TimerApplet" configuration="Debug" />
76 </Configuration>77 </Configuration>
77 <Configuration name="Release" ctype="CombineConfiguration">78 <Configuration name="Release" ctype="CombineConfiguration">
78 <Entry build="True" name="Rhythmbox" configuration="Release" />79 <Entry build="True" name="Rhythmbox" configuration="Release" />
@@ -147,6 +148,7 @@
147 <Entry build="True" name="Emesene" configuration="Release" />148 <Entry build="True" name="Emesene" configuration="Release" />
148 <Entry build="True" name="Dropbox" configuration="Release" />149 <Entry build="True" name="Dropbox" configuration="Release" />
149 <Entry build="True" name="RemindMe" configuration="Release" />150 <Entry build="True" name="RemindMe" configuration="Release" />
151 <Entry build="True" name="TimerApplet" configuration="Release" />
150 </Configuration>152 </Configuration>
151 </Configurations>153 </Configurations>
152 <StartMode startupentry="Rhythmbox" single="True">154 <StartMode startupentry="Rhythmbox" single="True">
@@ -222,6 +224,7 @@
222 <Execute type="None" entry="Emesene" />224 <Execute type="None" entry="Emesene" />
223 <Execute type="None" entry="Dropbox" />225 <Execute type="None" entry="Dropbox" />
224 <Execute type="None" entry="RemindMe" />226 <Execute type="None" entry="RemindMe" />
227 <Execute type="None" entry="TimerApplet" />
225 </StartMode>228 </StartMode>
226 <MonoDevelop.ChangeLogAddIn.ChangeLogInfo policy="UpdateNearestChangeLog" />229 <MonoDevelop.ChangeLogAddIn.ChangeLogInfo policy="UpdateNearestChangeLog" />
227 <Entries>230 <Entries>
@@ -297,5 +300,6 @@
297 <Entry filename="Emesene/Emesene.mdp" />300 <Entry filename="Emesene/Emesene.mdp" />
298 <Entry filename="Dropbox/Dropbox.mdp" />301 <Entry filename="Dropbox/Dropbox.mdp" />
299 <Entry filename="RemindMe/RemindMe.mdp" />302 <Entry filename="RemindMe/RemindMe.mdp" />
303 <Entry filename="TimerApplet/TimerApplet.mdp" />
300 </Entries>304 </Entries>
301</Combine>305</Combine>
302\ No newline at end of file306\ No newline at end of file
303307
=== modified file 'Makefile.am'
--- Makefile.am 2009-05-22 18:34:39 +0000
+++ Makefile.am 2009-06-11 21:13:44 +0000
@@ -64,6 +64,7 @@
64 TerminalServerClient \64 TerminalServerClient \
65 Text \65 Text \
66 Thunderbird \66 Thunderbird \
67 TimerApplet \
67 TinyUrl \68 TinyUrl \
68 Tracker \69 Tracker \
69 Translate \70 Translate \
7071
=== added directory 'TimerApplet'
=== added file 'TimerApplet/Makefile.am'
--- TimerApplet/Makefile.am 1970-01-01 00:00:00 +0000
+++ TimerApplet/Makefile.am 2009-06-11 21:13:44 +0000
@@ -0,0 +1,22 @@
1include $(top_srcdir)/build.rules.mk
2
3ASSEMBLY=TimerApplet
4
5FILES = \
6 src/PresetItem.cs \
7 src/TimerAppletItemSource.cs \
8 src/DBusTimerApplet.cs \
9 src/StartTimer.cs \
10 src/StopTimer.cs \
11 src/PauseContinueTimer.cs
12
13RESOURCES = \
14 Resources/TimerApplet.addin.xml
15
16REFERENCES = \
17 Mono.Posix \
18 System \
19 System.Core \
20 $(NDESK_DBUS_10_LIBS) \
21 $(DO_PLATFORM_LIBS) \
22 $(DO_UNIVERSE_LIBS)
023
=== added directory 'TimerApplet/Resources'
=== added file 'TimerApplet/Resources/TimerApplet.addin.xml'
--- TimerApplet/Resources/TimerApplet.addin.xml 1970-01-01 00:00:00 +0000
+++ TimerApplet/Resources/TimerApplet.addin.xml 2009-06-11 21:13:44 +0000
@@ -0,0 +1,28 @@
1<Addin
2 id="TimerApplet"
3 namespace="Do"
4 version="1.0"
5 name="TimerApplet"
6 description="Starting Timer-Applet Presets"
7 author="goto"
8 category="Community"
9 >
10
11 <Runtime>
12 <Import assembly="TimerApplet.dll"/>
13 </Runtime>
14
15 <Dependencies>
16 <Addin id="Universe" version="1.0" />
17 </Dependencies>
18
19 <!-- Extensions included in this assembly -->
20 <Extension path="/Do/ItemSource">
21 <ItemSource type="TimerApplet.TimerAppletItemSource"/>
22 </Extension>
23 <Extension path="/Do/Action">
24 <Action type="TimerApplet.StartTimer"/>
25 <Action type="TimerApplet.StopTimer"/>
26 <Action type="TimerApplet.PauseContinueTimer"/>
27 </Extension>
28</Addin>
029
=== added file 'TimerApplet/TimerApplet.mdp'
--- TimerApplet/TimerApplet.mdp 1970-01-01 00:00:00 +0000
+++ TimerApplet/TimerApplet.mdp 2009-06-11 21:13:44 +0000
@@ -0,0 +1,34 @@
1<Project name="TimerApplet" fileversion="2.0" language="C#" targetFramework="2.0" ctype="DotNetProject">
2 <Configurations active="Debug">
3 <Configuration name="Debug" ctype="DotNetProjectConfiguration">
4 <Output directory="bin/Debug" assembly="TimerApplet" />
5 <Build debugmode="True" target="Library" />
6 <Execution runwithwarnings="True" consolepause="False" runtime="MsNet" />
7 <CodeGeneration compiler="Mcs" warninglevel="4" optimize="False" unsafecodeallowed="False" generateoverflowchecks="False" definesymbols="DEBUG" generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
8 </Configuration>
9 <Configuration name="Release" ctype="DotNetProjectConfiguration">
10 <Output directory="bin/Release" assembly="TimerApplet" />
11 <Build debugmode="False" target="Library" />
12 <Execution runwithwarnings="True" consolepause="False" runtime="MsNet" />
13 <CodeGeneration compiler="Mcs" warninglevel="4" optimize="False" unsafecodeallowed="False" generateoverflowchecks="False" generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
14 </Configuration>
15 </Configurations>
16 <Contents>
17 <File name="Resources" subtype="Directory" buildaction="Compile" />
18 <File name="src" subtype="Directory" buildaction="Compile" />
19 <File name="Resources/TimerApplet.addin.xml" subtype="Code" buildaction="Nothing" />
20 <File name="src/PresetItem.cs" subtype="Code" buildaction="Compile" />
21 <File name="src/StartTimer.cs" subtype="Code" buildaction="Compile" />
22 <File name="src/TimerAppletItemSource.cs" subtype="Code" buildaction="Compile" />
23 <File name="src/DBusTimerApplet.cs" subtype="Code" buildaction="Compile" />
24 <File name="src/StopTimer.cs" subtype="Code" buildaction="Compile" />
25 <File name="src/PauseContinueTimer.cs" subtype="Code" buildaction="Compile" />
26 </Contents>
27 <References>
28 <ProjectReference type="Gac" localcopy="True" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
29 <ProjectReference type="Gac" localcopy="True" refto="Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
30 <ProjectReference type="Gac" localcopy="True" refto="Do.Platform, Version=0.9.0.0, Culture=neutral, PublicKeyToken=null" />
31 <ProjectReference type="Gac" localcopy="True" refto="Do.Universe, Version=0.9.0.0, Culture=neutral, PublicKeyToken=null" />
32 <ProjectReference type="Gac" localcopy="True" refto="NDesk.DBus, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f6716e4f9b2ed099" />
33 </References>
34</Project>
0\ No newline at end of file35\ No newline at end of file
136
=== added directory 'TimerApplet/src'
=== added file 'TimerApplet/src/DBusTimerApplet.cs'
--- TimerApplet/src/DBusTimerApplet.cs 1970-01-01 00:00:00 +0000
+++ TimerApplet/src/DBusTimerApplet.cs 2009-06-11 22:17:49 +0000
@@ -0,0 +1,108 @@
1using System;
2using System.Collections.Generic;
3using NDesk.DBus;
4using org.freedesktop.DBus;
5
6namespace TimerApplet
7{
8 [Interface("net.sourceforge.timerapplet.TimerApplet.Timer")]
9 public interface Timer
10 {
11 void Start (string name, int hours, int minutes, int seconds);
12 void Stop ();
13 void PauseContinue ();
14 }
15
16 public static class DBusTimerApplet
17 {
18 static Bus bus;
19 static string busName;
20 static ObjectPath path;
21 static Timer timer;
22 public struct ActualItem
23 {
24 public static PresetItem preset;
25 public static DateTime startTime;
26 public static DateTime endTime;
27 }
28
29 static DBusTimerApplet ()
30 {
31 bus = Bus.Session;
32 busName = "net.sourceforge.timerapplet.TimerApplet";
33 path = new ObjectPath("/net/sourceforge/timerapplet/TimerApplet/Timers/applet_3");
34 timer = null;
35 ActualItem.preset = null;
36
37 }
38 static bool HasOwner ()
39 {
40 if(!bus.NameHasOwner(busName)) {
41 bus.StartServiceByName(busName);
42 System.Threading.Thread.Sleep(5000);
43 }
44 if(bus.NameHasOwner(busName)) {
45 timer = bus.GetObject<Timer>(busName, path);
46 if(timer != null)
47 return true;
48 }
49 return false;
50 }
51 //Implementation of Timer.Start()
52 public static void Start (PresetItem preset)
53 {
54 if(HasOwner ()) {
55 if(preset != ActualItem.preset) {
56 if(ActualItem.preset != null) {
57 ActualItem.preset.Status = PresetStatus.None;
58 }
59 preset.Status = PresetStatus.Started;
60 ActualItem.preset = preset;
61 ActualItem.startTime = DateTime.Now;
62 ActualItem.endTime = DateTime.Now.Add(new TimeSpan (preset.Hours, preset.Minutes, preset.Seconds));
63 }
64 timer.Start(preset.Name, preset.Hours, preset.Minutes, preset.Seconds);
65 }
66 }
67 //Implementation of Timer.Stop()
68 public static void Stop ()
69 {
70 if(HasOwner ()) {
71 timer.Stop ();
72 ActualItem.preset.Status = PresetStatus.None;
73 ActualItem.preset = null;
74 }
75 }
76 //Implementation of Timer.PauseContinue()
77 public static void PauseContinue ()
78 {
79 if(HasOwner ()) {
80 timer.PauseContinue ();
81 if(ActualItem.preset.Status == PresetStatus.Started) {
82 ActualItem.preset.Status = PresetStatus.Paused;
83
84 TimeSpan lostTime = DateTime.Now - ActualItem.startTime;
85 ActualItem.preset.Hours -= lostTime.Hours;
86 ActualItem.preset.Minutes -= lostTime.Minutes;
87 ActualItem.preset.Seconds -= lostTime.Seconds;
88 }
89 else {
90 ActualItem.preset.Status = PresetStatus.Started;
91 ActualItem.endTime = DateTime.Now.Add (new TimeSpan(ActualItem.preset.Hours, ActualItem.preset.Minutes, ActualItem.preset.Seconds));
92 ActualItem.startTime = DateTime.Now;
93 }
94 }
95 }
96 public static bool TimerFinished ()
97 {
98 if(DBusTimerApplet.ActualItem.endTime > DateTime.Now)
99 return true;
100 else {
101 ActualItem.preset.Status = PresetStatus.None;
102 ActualItem.preset = null; // Timer finished => no actual item.
103 return false;
104 }
105 }
106
107 }
108}
0109
=== added file 'TimerApplet/src/PauseContinueTimer.cs'
--- TimerApplet/src/PauseContinueTimer.cs 1970-01-01 00:00:00 +0000
+++ TimerApplet/src/PauseContinueTimer.cs 2009-06-11 22:17:49 +0000
@@ -0,0 +1,45 @@
1using System;
2using System.Linq;
3using System.Collections.Generic;
4
5using Mono.Unix;
6
7using Do.Universe;
8
9namespace TimerApplet
10{
11 class PauseContinueTimer : Act
12 {
13 public override string Name {
14 get { return Catalog.GetString("Pause or Continue an Timer"); }
15 }
16 public override string Description {
17 get { return Catalog.GetString("Pauses or Continues the selected Timer"); }
18 }
19 public override string Icon {
20 get { return "timer-applet";}
21 }
22 public override IEnumerable<Type> SupportedItemTypes {
23 get { yield return typeof (PresetItem); }
24 }
25 public override bool SupportsItem (Item item) // Only supports Started or Paused Presets
26 {
27 if(item is PresetItem) {
28 PresetItem preset = (item as PresetItem);
29 if(preset == DBusTimerApplet.ActualItem.preset) {
30 if(preset.Status == PresetStatus.Started)
31 return DBusTimerApplet.TimerFinished();
32 if(preset.Status == PresetStatus.Paused)
33 return true;
34 }
35 }
36 return false;
37 }
38 public override IEnumerable<Item> Perform (IEnumerable<Item> items, IEnumerable<Item> modifierItems)
39 {
40 if(items.First () is PresetItem)
41 DBusTimerApplet.PauseContinue();
42 yield break;
43 }
44 }
45}
046
=== added file 'TimerApplet/src/PresetItem.cs'
--- TimerApplet/src/PresetItem.cs 1970-01-01 00:00:00 +0000
+++ TimerApplet/src/PresetItem.cs 2009-06-11 22:17:49 +0000
@@ -0,0 +1,53 @@
1using Do.Universe;
2
3namespace TimerApplet
4{
5 public enum PresetStatus
6 {
7 None,
8 Started,
9 Paused
10 }
11 public class PresetItem : Item
12 {
13 string name;
14 int hours;
15 int minutes;
16 int seconds;
17 PresetStatus status;
18
19 public PresetItem (string name, int hours, int minutes, int seconds)
20 {
21 this.name = name;
22 this.hours = hours;
23 this.minutes = minutes;
24 this.seconds = seconds;
25 this.status = PresetStatus.None;
26 }
27 public override string Name {
28 get { return string.Format ("{0} {1:D2}:{2:D2}:{3:D2}",name, hours, minutes, seconds); }
29 }
30 public int Hours {
31 get { return hours; }
32 set { hours = value; }
33 }
34 public int Minutes {
35 get { return minutes; }
36 set { minutes = value; }
37 }
38 public int Seconds {
39 get { return seconds; }
40 set { seconds = value; }
41 }
42 public PresetStatus Status {
43 get { return status; }
44 set { this.status = value; }
45 }
46 public override string Description {
47 get { return "Timer-Applet Preset: " + name; }
48 }
49 public override string Icon {
50 get { return "timer-applet"; }
51 }
52 }
53}
054
=== added file 'TimerApplet/src/StartTimer.cs'
--- TimerApplet/src/StartTimer.cs 1970-01-01 00:00:00 +0000
+++ TimerApplet/src/StartTimer.cs 2009-06-11 22:17:49 +0000
@@ -0,0 +1,36 @@
1using System;
2using System.Linq;
3using System.Collections.Generic;
4
5using Mono.Unix;
6
7using Do.Universe;
8
9namespace TimerApplet
10{
11 class StartTimer : Act
12 {
13 public override string Name {
14 get { return Catalog.GetString ("Start an Timer"); }
15 }
16 public override string Description {
17 get { return Catalog.GetString ("Starts the selected Timer"); }
18 }
19 public override string Icon {
20 get { return "timer-applet";}
21 }
22 public override IEnumerable<Type> SupportedItemTypes {
23 get { yield return typeof (PresetItem); }
24 }
25 public override bool SupportsItem (Item item) // Supports all Presets
26 {
27 return true;
28 }
29 public override IEnumerable<Item> Perform (IEnumerable<Item> items, IEnumerable<Item> modifierItems)
30 {
31 if(items.First () is PresetItem)
32 DBusTimerApplet.Start ((items.First () as PresetItem));
33 yield break;
34 }
35 }
36}
037
=== added file 'TimerApplet/src/StopTimer.cs'
--- TimerApplet/src/StopTimer.cs 1970-01-01 00:00:00 +0000
+++ TimerApplet/src/StopTimer.cs 2009-06-11 22:17:49 +0000
@@ -0,0 +1,46 @@
1using System;
2using System.Linq;
3using System.Collections.Generic;
4
5using Mono.Unix;
6
7using Do.Universe;
8
9namespace TimerApplet
10{
11 class StopTimer : Act
12 {
13 public override string Name {
14 get { return Catalog.GetString ("Stops an Timer"); }
15 }
16 public override string Description {
17 get { return Catalog.GetString ("Stops the selected Timer"); }
18 }
19 public override string Icon {
20 get { return "timer-applet"; }
21 }
22 public override IEnumerable<Type> SupportedItemTypes {
23 get { yield return typeof (PresetItem); }
24 }
25 public override bool SupportsItem (Item item) // Only supports Started or Paused Presets
26 {
27 if(item is PresetItem)
28 {
29 PresetItem preset = (item as PresetItem);
30 if(preset == DBusTimerApplet.ActualItem.preset) {
31 if(preset.Status == PresetStatus.Started)
32 return DBusTimerApplet.TimerFinished();
33 if(preset.Status == PresetStatus.Paused)
34 return true;
35 }
36 }
37 return false;
38 }
39 public override IEnumerable<Item> Perform (IEnumerable<Item> items, IEnumerable<Item> modifierItems)
40 {
41 if(items.First () is PresetItem)
42 DBusTimerApplet.Stop ();
43 yield break;
44 }
45 }
46}
047
=== added file 'TimerApplet/src/TimerAppletItemSource.cs'
--- TimerApplet/src/TimerAppletItemSource.cs 1970-01-01 00:00:00 +0000
+++ TimerApplet/src/TimerAppletItemSource.cs 2009-06-11 22:17:49 +0000
@@ -0,0 +1,65 @@
1using System;
2using System.IO;
3using System.Collections.Generic;
4using System.Text.RegularExpressions;
5
6using Mono.Unix;
7
8using Do.Platform;
9using Do.Universe;
10using Do.Universe.Common;
11
12namespace TimerApplet
13{
14 class TimerAppletItemSource : ItemSource
15 {
16 List<Item> items;
17
18 public TimerAppletItemSource ()
19 {
20 items = new List<Item> ();
21 }
22
23 public override string Name {
24 get { return Catalog.GetString ("Timer-Applet"); }
25 }
26 public override string Description {
27 get { return Catalog.GetString ("Starting Timer-Applet Presets"); }
28 }
29 public override string Icon {
30 get { return "timer-applet"; }
31 }
32 public override IEnumerable<Type> SupportedItemTypes {
33 get { yield return typeof (Item); }
34 }
35 public override IEnumerable<Item> Items {
36 get { return items; }
37 }
38
39 public override void UpdateItems ()
40 {
41 string home = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
42 string path = "~/.gnome2/timer-applet/presets.xml".Replace ("~", home);
43
44 items.Clear ();
45 try {
46 using(StreamReader streamReader = new StreamReader (path)) {
47 string file = streamReader.ReadToEnd ();
48 Regex regex = new Regex ("<preset duration=\"([0-9]*?)\" name=\"(.*?)\" />");
49 foreach(Match match in regex.Matches (file)) {
50 string name = match.Groups [2].ToString ();
51 int time = Convert.ToInt32(match.Groups [1].ToString ());
52 int seconds = time % 60;
53 int minutes = (time % 3600) / 60;
54 int hour = time / 3600;
55 items.Add(new PresetItem (name, hour, minutes, seconds));
56 }
57 }
58 }
59 catch(Exception e) {
60 Log.Error ("Could not read Presets file {0}: {1}", path, e.Message);
61 Log.Debug (e.StackTrace);
62 }
63 }
64 }
65}
066
=== modified file 'configure.ac'
--- configure.ac 2009-05-22 18:34:39 +0000
+++ configure.ac 2009-06-11 21:13:44 +0000
@@ -301,6 +301,7 @@
301TerminalServerClient/Makefile301TerminalServerClient/Makefile
302Text/Makefile302Text/Makefile
303Thunderbird/Makefile303Thunderbird/Makefile
304TimerApplet/Makefile
304TinyUrl/Makefile305TinyUrl/Makefile
305Tracker/Makefile306Tracker/Makefile
306Translate/Makefile307Translate/Makefile

Subscribers

People subscribed via source and target branches