[Tkabber-dev] r256 - trunk/plugins/reminder

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Fri Dec 25 06:57:58 MSK 2009


Author: bigote
Date: 2009-12-25 06:57:57 +0300 (Fri, 25 Dec 2009)
New Revision: 256

Modified:
   trunk/plugins/reminder/reminder.tcl
Log:
Added code to clean up expired reminders:
-- when adding a new reminder;
-- when printing a list of current reminders.
TODO: it would be nice to remove an expired reminder exactly
when hitting the OK button of a popped-up reminder dialog.


Modified: trunk/plugins/reminder/reminder.tcl
===================================================================
--- trunk/plugins/reminder/reminder.tcl	2009-12-23 02:50:21 UTC (rev 255)
+++ trunk/plugins/reminder/reminder.tcl	2009-12-25 03:57:57 UTC (rev 256)
@@ -1,4 +1,4 @@
-# "Reminder" plugin for Tkabber. 2009-12-23 v. 0.2
+# "Reminder" plugin for Tkabber. 2009-12-25 v. 0.2.1
 # Written by Serge Yudin xmpp:bigote at jabber.ru
 # Its purpose is to remind about your contact birthdays automatically
 # and about some tasks that you assign by means of "/remind" command.
@@ -22,8 +22,6 @@
 #####################################
 #/listrems
 # This will show you a list of all running reminders you have set.
-# It works but
-# TODO: keeps old reminders without deleting expired ones from the list.
 #####################################
 #
 # TODO: At the moment the commands only work in MUC windows.
@@ -57,7 +55,7 @@
 
 proc reminder::load {} {
     variable rem_list
-	variable rem_number 1
+	variable rem_number
     variable winid 0
     hook::add generate_completions_hook \
     [namespace current]::reminder_compls
@@ -88,7 +86,6 @@
 
 proc reminder::handle_commands {chatid user body type} {
     variable rem_list
-	variable rem_number
 # Detect the command.
     if {[string match {/remind*} $body]} {
         set cmd /remind
@@ -108,17 +105,32 @@
 		    if {[string match *:* $timeout]} {
 			    [namespace current]::parse_timeout $timeout
 			}
-            set exp_date [clock format [expr {$timeout + [clock seconds]}]]
-			set listrem_item [list $rem_number\tExpires:\ $exp_date\t\"$message\"]
+			# This If removes expired reminders, if any.
+			if {[info exists rem_list]} {
+				set rem_number 1
+				set rl_1 $rem_list
+				foreach lri $rem_list {
+					if {[clock seconds]<[lindex $lri 1]} {
+					    incr rem_number
+					} else {
+						set rl_1 [lreplace $rem_list $rem_number-1 $rem_number-1]
+					}
+				}
+				set rem_list $rl_1
+			}
+            set rid [after [expr {1000 * $timeout}] \
+            [namespace current]::show_reminder [list $message]]
+            set exp_date [expr {$timeout + [clock seconds]}]
+			lappend listrem_item $rid $exp_date "$message"
 			lappend rem_list $listrem_item
-			incr rem_number
-            after [expr {1000 * $timeout}] \
-            [namespace current]::show_reminder [list $message]
         }
     }
     return stop
 }
 
+# TODO: If I manage to pass $rid here it would be nice to delete a reminder
+# from the list right here after its expiration (when hitting OK).
+# However, it's possible to call a proc that would check all reminders.
 proc reminder::show_reminder {msg} {
     variable winid
     set w .reminder$winid
@@ -139,7 +151,17 @@
     variable rem_list
     set group [chat::get_jid $chatid]
     chat::add_message $chatid $group info "List of current reminders:" {}
+	set rem_number 1
+	set rl_1 $rem_list
+# Checking for expired reminders and removing them, if any.
 	foreach lri $rem_list {
-        chat::add_message $chatid $group info "[join $lri]" {}
+		if {[clock seconds]<[lindex $lri 1]} {
+	        set msg [list $rem_number\tExpires:\ [clock format [lindex $lri 1]] [lindex $lri 2]]
+            chat::add_message $chatid $group info "[join $msg]" {}
+		    incr rem_number
+		} else {
+		    set rl_1 [lreplace $rem_list $rem_number-1 $rem_number-1]
+		}
 	}
+	set rem_list $rl_1
 }



More information about the Tkabber-dev mailing list