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
1=== modified file 'DoPlugins.mds'
2--- DoPlugins.mds 2009-06-11 07:36:57 +0000
3+++ DoPlugins.mds 2009-06-11 22:04:04 +0000
4@@ -73,6 +73,7 @@
5 <Entry build="True" name="Emesene" configuration="Debug" />
6 <Entry build="True" name="Dropbox" configuration="Debug" />
7 <Entry build="True" name="RemindMe" configuration="Debug" />
8+ <Entry build="True" name="TimerApplet" configuration="Debug" />
9 </Configuration>
10 <Configuration name="Release" ctype="CombineConfiguration">
11 <Entry build="True" name="Rhythmbox" configuration="Release" />
12@@ -147,6 +148,7 @@
13 <Entry build="True" name="Emesene" configuration="Release" />
14 <Entry build="True" name="Dropbox" configuration="Release" />
15 <Entry build="True" name="RemindMe" configuration="Release" />
16+ <Entry build="True" name="TimerApplet" configuration="Release" />
17 </Configuration>
18 </Configurations>
19 <StartMode startupentry="Rhythmbox" single="True">
20@@ -222,6 +224,7 @@
21 <Execute type="None" entry="Emesene" />
22 <Execute type="None" entry="Dropbox" />
23 <Execute type="None" entry="RemindMe" />
24+ <Execute type="None" entry="TimerApplet" />
25 </StartMode>
26 <MonoDevelop.ChangeLogAddIn.ChangeLogInfo policy="UpdateNearestChangeLog" />
27 <Entries>
28@@ -297,5 +300,6 @@
29 <Entry filename="Emesene/Emesene.mdp" />
30 <Entry filename="Dropbox/Dropbox.mdp" />
31 <Entry filename="RemindMe/RemindMe.mdp" />
32+ <Entry filename="TimerApplet/TimerApplet.mdp" />
33 </Entries>
34 </Combine>
35\ No newline at end of file
36
37=== modified file 'Makefile.am'
38--- Makefile.am 2009-05-22 18:34:39 +0000
39+++ Makefile.am 2009-06-11 21:13:44 +0000
40@@ -64,6 +64,7 @@
41 TerminalServerClient \
42 Text \
43 Thunderbird \
44+ TimerApplet \
45 TinyUrl \
46 Tracker \
47 Translate \
48
49=== added directory 'TimerApplet'
50=== added file 'TimerApplet/Makefile.am'
51--- TimerApplet/Makefile.am 1970-01-01 00:00:00 +0000
52+++ TimerApplet/Makefile.am 2009-06-11 21:13:44 +0000
53@@ -0,0 +1,22 @@
54+include $(top_srcdir)/build.rules.mk
55+
56+ASSEMBLY=TimerApplet
57+
58+FILES = \
59+ src/PresetItem.cs \
60+ src/TimerAppletItemSource.cs \
61+ src/DBusTimerApplet.cs \
62+ src/StartTimer.cs \
63+ src/StopTimer.cs \
64+ src/PauseContinueTimer.cs
65+
66+RESOURCES = \
67+ Resources/TimerApplet.addin.xml
68+
69+REFERENCES = \
70+ Mono.Posix \
71+ System \
72+ System.Core \
73+ $(NDESK_DBUS_10_LIBS) \
74+ $(DO_PLATFORM_LIBS) \
75+ $(DO_UNIVERSE_LIBS)
76
77=== added directory 'TimerApplet/Resources'
78=== added file 'TimerApplet/Resources/TimerApplet.addin.xml'
79--- TimerApplet/Resources/TimerApplet.addin.xml 1970-01-01 00:00:00 +0000
80+++ TimerApplet/Resources/TimerApplet.addin.xml 2009-06-11 21:13:44 +0000
81@@ -0,0 +1,28 @@
82+<Addin
83+ id="TimerApplet"
84+ namespace="Do"
85+ version="1.0"
86+ name="TimerApplet"
87+ description="Starting Timer-Applet Presets"
88+ author="goto"
89+ category="Community"
90+ >
91+
92+ <Runtime>
93+ <Import assembly="TimerApplet.dll"/>
94+ </Runtime>
95+
96+ <Dependencies>
97+ <Addin id="Universe" version="1.0" />
98+ </Dependencies>
99+
100+ <!-- Extensions included in this assembly -->
101+ <Extension path="/Do/ItemSource">
102+ <ItemSource type="TimerApplet.TimerAppletItemSource"/>
103+ </Extension>
104+ <Extension path="/Do/Action">
105+ <Action type="TimerApplet.StartTimer"/>
106+ <Action type="TimerApplet.StopTimer"/>
107+ <Action type="TimerApplet.PauseContinueTimer"/>
108+ </Extension>
109+</Addin>
110
111=== added file 'TimerApplet/TimerApplet.mdp'
112--- TimerApplet/TimerApplet.mdp 1970-01-01 00:00:00 +0000
113+++ TimerApplet/TimerApplet.mdp 2009-06-11 21:13:44 +0000
114@@ -0,0 +1,34 @@
115+<Project name="TimerApplet" fileversion="2.0" language="C#" targetFramework="2.0" ctype="DotNetProject">
116+ <Configurations active="Debug">
117+ <Configuration name="Debug" ctype="DotNetProjectConfiguration">
118+ <Output directory="bin/Debug" assembly="TimerApplet" />
119+ <Build debugmode="True" target="Library" />
120+ <Execution runwithwarnings="True" consolepause="False" runtime="MsNet" />
121+ <CodeGeneration compiler="Mcs" warninglevel="4" optimize="False" unsafecodeallowed="False" generateoverflowchecks="False" definesymbols="DEBUG" generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
122+ </Configuration>
123+ <Configuration name="Release" ctype="DotNetProjectConfiguration">
124+ <Output directory="bin/Release" assembly="TimerApplet" />
125+ <Build debugmode="False" target="Library" />
126+ <Execution runwithwarnings="True" consolepause="False" runtime="MsNet" />
127+ <CodeGeneration compiler="Mcs" warninglevel="4" optimize="False" unsafecodeallowed="False" generateoverflowchecks="False" generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
128+ </Configuration>
129+ </Configurations>
130+ <Contents>
131+ <File name="Resources" subtype="Directory" buildaction="Compile" />
132+ <File name="src" subtype="Directory" buildaction="Compile" />
133+ <File name="Resources/TimerApplet.addin.xml" subtype="Code" buildaction="Nothing" />
134+ <File name="src/PresetItem.cs" subtype="Code" buildaction="Compile" />
135+ <File name="src/StartTimer.cs" subtype="Code" buildaction="Compile" />
136+ <File name="src/TimerAppletItemSource.cs" subtype="Code" buildaction="Compile" />
137+ <File name="src/DBusTimerApplet.cs" subtype="Code" buildaction="Compile" />
138+ <File name="src/StopTimer.cs" subtype="Code" buildaction="Compile" />
139+ <File name="src/PauseContinueTimer.cs" subtype="Code" buildaction="Compile" />
140+ </Contents>
141+ <References>
142+ <ProjectReference type="Gac" localcopy="True" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
143+ <ProjectReference type="Gac" localcopy="True" refto="Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
144+ <ProjectReference type="Gac" localcopy="True" refto="Do.Platform, Version=0.9.0.0, Culture=neutral, PublicKeyToken=null" />
145+ <ProjectReference type="Gac" localcopy="True" refto="Do.Universe, Version=0.9.0.0, Culture=neutral, PublicKeyToken=null" />
146+ <ProjectReference type="Gac" localcopy="True" refto="NDesk.DBus, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f6716e4f9b2ed099" />
147+ </References>
148+</Project>
149\ No newline at end of file
150
151=== added directory 'TimerApplet/src'
152=== added file 'TimerApplet/src/DBusTimerApplet.cs'
153--- TimerApplet/src/DBusTimerApplet.cs 1970-01-01 00:00:00 +0000
154+++ TimerApplet/src/DBusTimerApplet.cs 2009-06-11 22:17:49 +0000
155@@ -0,0 +1,108 @@
156+using System;
157+using System.Collections.Generic;
158+using NDesk.DBus;
159+using org.freedesktop.DBus;
160+
161+namespace TimerApplet
162+{
163+ [Interface("net.sourceforge.timerapplet.TimerApplet.Timer")]
164+ public interface Timer
165+ {
166+ void Start (string name, int hours, int minutes, int seconds);
167+ void Stop ();
168+ void PauseContinue ();
169+ }
170+
171+ public static class DBusTimerApplet
172+ {
173+ static Bus bus;
174+ static string busName;
175+ static ObjectPath path;
176+ static Timer timer;
177+ public struct ActualItem
178+ {
179+ public static PresetItem preset;
180+ public static DateTime startTime;
181+ public static DateTime endTime;
182+ }
183+
184+ static DBusTimerApplet ()
185+ {
186+ bus = Bus.Session;
187+ busName = "net.sourceforge.timerapplet.TimerApplet";
188+ path = new ObjectPath("/net/sourceforge/timerapplet/TimerApplet/Timers/applet_3");
189+ timer = null;
190+ ActualItem.preset = null;
191+
192+ }
193+ static bool HasOwner ()
194+ {
195+ if(!bus.NameHasOwner(busName)) {
196+ bus.StartServiceByName(busName);
197+ System.Threading.Thread.Sleep(5000);
198+ }
199+ if(bus.NameHasOwner(busName)) {
200+ timer = bus.GetObject<Timer>(busName, path);
201+ if(timer != null)
202+ return true;
203+ }
204+ return false;
205+ }
206+ //Implementation of Timer.Start()
207+ public static void Start (PresetItem preset)
208+ {
209+ if(HasOwner ()) {
210+ if(preset != ActualItem.preset) {
211+ if(ActualItem.preset != null) {
212+ ActualItem.preset.Status = PresetStatus.None;
213+ }
214+ preset.Status = PresetStatus.Started;
215+ ActualItem.preset = preset;
216+ ActualItem.startTime = DateTime.Now;
217+ ActualItem.endTime = DateTime.Now.Add(new TimeSpan (preset.Hours, preset.Minutes, preset.Seconds));
218+ }
219+ timer.Start(preset.Name, preset.Hours, preset.Minutes, preset.Seconds);
220+ }
221+ }
222+ //Implementation of Timer.Stop()
223+ public static void Stop ()
224+ {
225+ if(HasOwner ()) {
226+ timer.Stop ();
227+ ActualItem.preset.Status = PresetStatus.None;
228+ ActualItem.preset = null;
229+ }
230+ }
231+ //Implementation of Timer.PauseContinue()
232+ public static void PauseContinue ()
233+ {
234+ if(HasOwner ()) {
235+ timer.PauseContinue ();
236+ if(ActualItem.preset.Status == PresetStatus.Started) {
237+ ActualItem.preset.Status = PresetStatus.Paused;
238+
239+ TimeSpan lostTime = DateTime.Now - ActualItem.startTime;
240+ ActualItem.preset.Hours -= lostTime.Hours;
241+ ActualItem.preset.Minutes -= lostTime.Minutes;
242+ ActualItem.preset.Seconds -= lostTime.Seconds;
243+ }
244+ else {
245+ ActualItem.preset.Status = PresetStatus.Started;
246+ ActualItem.endTime = DateTime.Now.Add (new TimeSpan(ActualItem.preset.Hours, ActualItem.preset.Minutes, ActualItem.preset.Seconds));
247+ ActualItem.startTime = DateTime.Now;
248+ }
249+ }
250+ }
251+ public static bool TimerFinished ()
252+ {
253+ if(DBusTimerApplet.ActualItem.endTime > DateTime.Now)
254+ return true;
255+ else {
256+ ActualItem.preset.Status = PresetStatus.None;
257+ ActualItem.preset = null; // Timer finished => no actual item.
258+ return false;
259+ }
260+ }
261+
262+ }
263+}
264
265=== added file 'TimerApplet/src/PauseContinueTimer.cs'
266--- TimerApplet/src/PauseContinueTimer.cs 1970-01-01 00:00:00 +0000
267+++ TimerApplet/src/PauseContinueTimer.cs 2009-06-11 22:17:49 +0000
268@@ -0,0 +1,45 @@
269+using System;
270+using System.Linq;
271+using System.Collections.Generic;
272+
273+using Mono.Unix;
274+
275+using Do.Universe;
276+
277+namespace TimerApplet
278+{
279+ class PauseContinueTimer : Act
280+ {
281+ public override string Name {
282+ get { return Catalog.GetString("Pause or Continue an Timer"); }
283+ }
284+ public override string Description {
285+ get { return Catalog.GetString("Pauses or Continues the selected Timer"); }
286+ }
287+ public override string Icon {
288+ get { return "timer-applet";}
289+ }
290+ public override IEnumerable<Type> SupportedItemTypes {
291+ get { yield return typeof (PresetItem); }
292+ }
293+ public override bool SupportsItem (Item item) // Only supports Started or Paused Presets
294+ {
295+ if(item is PresetItem) {
296+ PresetItem preset = (item as PresetItem);
297+ if(preset == DBusTimerApplet.ActualItem.preset) {
298+ if(preset.Status == PresetStatus.Started)
299+ return DBusTimerApplet.TimerFinished();
300+ if(preset.Status == PresetStatus.Paused)
301+ return true;
302+ }
303+ }
304+ return false;
305+ }
306+ public override IEnumerable<Item> Perform (IEnumerable<Item> items, IEnumerable<Item> modifierItems)
307+ {
308+ if(items.First () is PresetItem)
309+ DBusTimerApplet.PauseContinue();
310+ yield break;
311+ }
312+ }
313+}
314
315=== added file 'TimerApplet/src/PresetItem.cs'
316--- TimerApplet/src/PresetItem.cs 1970-01-01 00:00:00 +0000
317+++ TimerApplet/src/PresetItem.cs 2009-06-11 22:17:49 +0000
318@@ -0,0 +1,53 @@
319+using Do.Universe;
320+
321+namespace TimerApplet
322+{
323+ public enum PresetStatus
324+ {
325+ None,
326+ Started,
327+ Paused
328+ }
329+ public class PresetItem : Item
330+ {
331+ string name;
332+ int hours;
333+ int minutes;
334+ int seconds;
335+ PresetStatus status;
336+
337+ public PresetItem (string name, int hours, int minutes, int seconds)
338+ {
339+ this.name = name;
340+ this.hours = hours;
341+ this.minutes = minutes;
342+ this.seconds = seconds;
343+ this.status = PresetStatus.None;
344+ }
345+ public override string Name {
346+ get { return string.Format ("{0} {1:D2}:{2:D2}:{3:D2}",name, hours, minutes, seconds); }
347+ }
348+ public int Hours {
349+ get { return hours; }
350+ set { hours = value; }
351+ }
352+ public int Minutes {
353+ get { return minutes; }
354+ set { minutes = value; }
355+ }
356+ public int Seconds {
357+ get { return seconds; }
358+ set { seconds = value; }
359+ }
360+ public PresetStatus Status {
361+ get { return status; }
362+ set { this.status = value; }
363+ }
364+ public override string Description {
365+ get { return "Timer-Applet Preset: " + name; }
366+ }
367+ public override string Icon {
368+ get { return "timer-applet"; }
369+ }
370+ }
371+}
372
373=== added file 'TimerApplet/src/StartTimer.cs'
374--- TimerApplet/src/StartTimer.cs 1970-01-01 00:00:00 +0000
375+++ TimerApplet/src/StartTimer.cs 2009-06-11 22:17:49 +0000
376@@ -0,0 +1,36 @@
377+using System;
378+using System.Linq;
379+using System.Collections.Generic;
380+
381+using Mono.Unix;
382+
383+using Do.Universe;
384+
385+namespace TimerApplet
386+{
387+ class StartTimer : Act
388+ {
389+ public override string Name {
390+ get { return Catalog.GetString ("Start an Timer"); }
391+ }
392+ public override string Description {
393+ get { return Catalog.GetString ("Starts the selected Timer"); }
394+ }
395+ public override string Icon {
396+ get { return "timer-applet";}
397+ }
398+ public override IEnumerable<Type> SupportedItemTypes {
399+ get { yield return typeof (PresetItem); }
400+ }
401+ public override bool SupportsItem (Item item) // Supports all Presets
402+ {
403+ return true;
404+ }
405+ public override IEnumerable<Item> Perform (IEnumerable<Item> items, IEnumerable<Item> modifierItems)
406+ {
407+ if(items.First () is PresetItem)
408+ DBusTimerApplet.Start ((items.First () as PresetItem));
409+ yield break;
410+ }
411+ }
412+}
413
414=== added file 'TimerApplet/src/StopTimer.cs'
415--- TimerApplet/src/StopTimer.cs 1970-01-01 00:00:00 +0000
416+++ TimerApplet/src/StopTimer.cs 2009-06-11 22:17:49 +0000
417@@ -0,0 +1,46 @@
418+using System;
419+using System.Linq;
420+using System.Collections.Generic;
421+
422+using Mono.Unix;
423+
424+using Do.Universe;
425+
426+namespace TimerApplet
427+{
428+ class StopTimer : Act
429+ {
430+ public override string Name {
431+ get { return Catalog.GetString ("Stops an Timer"); }
432+ }
433+ public override string Description {
434+ get { return Catalog.GetString ("Stops the selected Timer"); }
435+ }
436+ public override string Icon {
437+ get { return "timer-applet"; }
438+ }
439+ public override IEnumerable<Type> SupportedItemTypes {
440+ get { yield return typeof (PresetItem); }
441+ }
442+ public override bool SupportsItem (Item item) // Only supports Started or Paused Presets
443+ {
444+ if(item is PresetItem)
445+ {
446+ PresetItem preset = (item as PresetItem);
447+ if(preset == DBusTimerApplet.ActualItem.preset) {
448+ if(preset.Status == PresetStatus.Started)
449+ return DBusTimerApplet.TimerFinished();
450+ if(preset.Status == PresetStatus.Paused)
451+ return true;
452+ }
453+ }
454+ return false;
455+ }
456+ public override IEnumerable<Item> Perform (IEnumerable<Item> items, IEnumerable<Item> modifierItems)
457+ {
458+ if(items.First () is PresetItem)
459+ DBusTimerApplet.Stop ();
460+ yield break;
461+ }
462+ }
463+}
464
465=== added file 'TimerApplet/src/TimerAppletItemSource.cs'
466--- TimerApplet/src/TimerAppletItemSource.cs 1970-01-01 00:00:00 +0000
467+++ TimerApplet/src/TimerAppletItemSource.cs 2009-06-11 22:17:49 +0000
468@@ -0,0 +1,65 @@
469+using System;
470+using System.IO;
471+using System.Collections.Generic;
472+using System.Text.RegularExpressions;
473+
474+using Mono.Unix;
475+
476+using Do.Platform;
477+using Do.Universe;
478+using Do.Universe.Common;
479+
480+namespace TimerApplet
481+{
482+ class TimerAppletItemSource : ItemSource
483+ {
484+ List<Item> items;
485+
486+ public TimerAppletItemSource ()
487+ {
488+ items = new List<Item> ();
489+ }
490+
491+ public override string Name {
492+ get { return Catalog.GetString ("Timer-Applet"); }
493+ }
494+ public override string Description {
495+ get { return Catalog.GetString ("Starting Timer-Applet Presets"); }
496+ }
497+ public override string Icon {
498+ get { return "timer-applet"; }
499+ }
500+ public override IEnumerable<Type> SupportedItemTypes {
501+ get { yield return typeof (Item); }
502+ }
503+ public override IEnumerable<Item> Items {
504+ get { return items; }
505+ }
506+
507+ public override void UpdateItems ()
508+ {
509+ string home = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
510+ string path = "~/.gnome2/timer-applet/presets.xml".Replace ("~", home);
511+
512+ items.Clear ();
513+ try {
514+ using(StreamReader streamReader = new StreamReader (path)) {
515+ string file = streamReader.ReadToEnd ();
516+ Regex regex = new Regex ("<preset duration=\"([0-9]*?)\" name=\"(.*?)\" />");
517+ foreach(Match match in regex.Matches (file)) {
518+ string name = match.Groups [2].ToString ();
519+ int time = Convert.ToInt32(match.Groups [1].ToString ());
520+ int seconds = time % 60;
521+ int minutes = (time % 3600) / 60;
522+ int hour = time / 3600;
523+ items.Add(new PresetItem (name, hour, minutes, seconds));
524+ }
525+ }
526+ }
527+ catch(Exception e) {
528+ Log.Error ("Could not read Presets file {0}: {1}", path, e.Message);
529+ Log.Debug (e.StackTrace);
530+ }
531+ }
532+ }
533+}
534
535=== modified file 'configure.ac'
536--- configure.ac 2009-05-22 18:34:39 +0000
537+++ configure.ac 2009-06-11 21:13:44 +0000
538@@ -301,6 +301,7 @@
539 TerminalServerClient/Makefile
540 Text/Makefile
541 Thunderbird/Makefile
542+TimerApplet/Makefile
543 TinyUrl/Makefile
544 Tracker/Makefile
545 Translate/Makefile

Subscribers

People subscribed via source and target branches