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

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Fri Jan 1 21:18:30 MSK 2010


Author: bigote
Date: 2010-01-01 21:18:29 +0300 (Fri, 01 Jan 2010)
New Revision: 263

Modified:
   trunk/plugins/reminder/reminder.tcl
Log:
Added saving and restoring reminders to and form file.


Modified: trunk/plugins/reminder/reminder.tcl
===================================================================
--- trunk/plugins/reminder/reminder.tcl	2009-12-30 05:11:18 UTC (rev 262)
+++ trunk/plugins/reminder/reminder.tcl	2010-01-01 18:18:29 UTC (rev 263)
@@ -1,4 +1,4 @@
-# "Reminder" plugin for Tkabber. 2009-12-30 v. 0.7
+# "Reminder" plugin for Tkabber. 2010-01-01 v. 0.8
 # 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.
@@ -8,8 +8,7 @@
 # either take a look at reminder::help procedure here.
 #####################################
 #
-# TODO: Add restoring long term reminders (their saving works already).
-# TODO: Add reminders saving on closing Tkabber.
+# TODO: Rewrite storing and restoing reminders via custom::defvar
 # TODO: Add some sound reminder notification.
 # TODO: Birthday reminding isn't ready yet, at all.
 #
@@ -48,6 +47,7 @@
     variable rem_list
     variable rem_number
     variable winid 0
+    [namespace current]::restore_reminders
     hook::add generate_completions_hook \
     [namespace current]::reminder_compls
     hook::add chat_send_message_hook \
@@ -109,7 +109,7 @@
                 min (minute), tomorrow." {}
                 return stop
             } elseif {$timeout == -3} {
-                chat::add_message $chatid $group error "You've set something really odd ;-)" {}
+                chat::add_message $chatid $group error "You set something really odd ;-)" {}
                 return stop
             } elseif {[string equal -nocase $timeout help]} {
                 return stop
@@ -118,16 +118,8 @@
                 "It seems that you have set your reminder to some date in the past." {}
                 return stop
             }
-            set timeout [string trimleft $timeout 0]
-            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
+            [namespace current]::set_reminder $timeout $message
             [namespace current]::list_reminders $chatid
-            if {$timeout > 300} {
-                [namespace current]::save_rem
-            }
         }
         /delrem {
             if {[info exists rem_list] && [llength $rem_list] > 0} {
@@ -145,15 +137,25 @@
             } else {
                 return stop
             }
-            [namespace current]::save_rem
+            [namespace current]::save_reminders
         }
     }
     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::set_reminder {timeout message} {
+    variable rem_list
+    set timeout [string trimleft $timeout 0]
+    set rid [after [expr {1000 * $timeout}] \
+    [namespace current]::show_reminder [list $message]]
+    set exp_date [expr {$timeout + [clock seconds]}]
+    lappend lri $rid $exp_date "$message"
+    lappend rem_list $lri
+    if {$timeout > 300} {
+        [namespace current]::save_reminders
+    }
+}
+
 proc reminder::show_reminder {msg} {
     variable winid
     set w .reminder$winid
@@ -161,11 +163,13 @@
     if {[winfo exists $w]} {
         destroy $w
     }
-    set msg [clock format [clock seconds]]\n$msg
+    set msg "Now: [clock format [clock seconds]]\n$msg"
     Dialog $w -title [::msgcat::mc "Reminder"] \
         -modal none -separator 1 -anchor e -default 0 -parent .
         set f [$w getframe]
-        $w add -text [::msgcat::mc "OK"] -command [list destroy $w]
+        $w add -text [::msgcat::mc "OK"] -command "destroy $w; \
+        [namespace current]::clean_rem_list; \
+        [namespace current]::save_reminders"
         label $w.lsure -padx 10 -pady 5 -text [::msgcat::mc $msg]
         pack $w.lsure -expand yes -fill both
         $w draw
@@ -173,25 +177,36 @@
 
 proc reminder::list_reminders {chatid} {
     variable rem_list
+    variable rem_number
     set group [chat::get_jid $chatid]
     chat::add_message $chatid $group info "List of current reminders:" {}
     if {![info exists rem_list]} {
         chat::add_message $chatid $group info "No reminders were set yet." {}
         return stop
     }
-    set rem_number 1
+    [namespace current]::clean_rem_list
+    foreach lri $rem_list {
+        set msg [list $rem_number\tExpires:\ [clock format [lindex $lri 1]] [lindex $lri 2]]
+        chat::add_message $chatid $group info "[join $msg]" {}
+    }
+    [namespace current]::save_reminders
+}
+
+proc reminder::clean_rem_list {} {
+    variable rem_list
+    variable rem_number
+    set r_number 1
     set rl_1 $rem_list
 # Checking for expired reminders and removing them, if any.
     foreach lri $rem_list {
         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
+        incr r_number
         } else {
-            set rl_1 [lreplace $rem_list $rem_number-1 $rem_number-1]
+            set rl_1 [lreplace $rem_list $r_number-1 $r_number-1]
         }
     }
     set rem_list $rl_1
+    set rem_number $r_number
 }
 
 proc reminder::parse_timeout {to chatid} {
@@ -291,14 +306,13 @@
         \n\nAll reminders with timeout larger than 5 minutes are stored into a file.\
         \nA list of current reminders is saved automatically after having set a new reminder,\
         \nafter deleting one, and just before closing Tkabber (yet to be implemented).\
-        \nThe list is supposed to be restored from the file after Tkabber's start.\
-        \nHowever, this part doesn't work yet ;-)"
+        \nThe list is restored from the file after Tkabber's start."
     chat::add_message $chatid $group error "Some examples of using /remind command:" {}
     chat::add_message $chatid $group info "[join $help_msg]" {}
     return stop
 }
 
-proc reminder::save_rem {} {
+proc reminder::save_reminders {} {
     variable options
     variable rem_list
     set log $options(reminders_file)
@@ -311,23 +325,18 @@
         return
     }
     foreach rem $rem_list {
-puts \"$rem\"
         lappend to_save \n$rem
-puts $to_save
     }
-    puts $fd "[join $to_save]"
-    close $fd
+    if {[info exists to_save]} {
+        puts $fd "[join $to_save]"
+        close $fd
+    }
 }
 
-# Don't forget to count new timeout, for the current time has changed.
-# Check all saved reminders for being expired. If there's such reminders,
-# these are to be shown at once. The rest of them should be started
-# as new reminders with new timeouts.
-proc reminder::load_rem {} {
+proc reminder::restore_reminders {} {
+    variable options
     variable rem_list
-    variable options
     set log $options(reminders_file)
-puts $log
     if { $log eq "" } {
         return
     }
@@ -338,16 +347,26 @@
             return
         } else {
             set list [split [read $fd] \n]
+            close $fd
             foreach i $list {
                 if {$i != ""} {
-                    lappend rem_list [string trim $i]
+# Counting new timeout, for the current time has changed.
+                    set old_time [lindex $i 1]
+                    set to [expr $old_time - [clock seconds]]
+                    set msg [lindex $i 2]
+# Check all saved reminders for expired ones.
+                    if {$to > 0} {
+                        [namespace current]::set_reminder $to $msg
+                    } else {
+# If there are expired reminders, they are to be shown at once.
+                        lappend rem_list $i
+                        set msg "Old reminder. Expired: [clock format $old_time]\n$msg"
+                        after idle [namespace current]::show_reminder [list $msg]
+                    }
                 }
             }
-puts $rem_list
         }
     } else {
         return
     }
 }
-
-hook::add postload_hook reminder::load_rem 100



More information about the Tkabber-dev mailing list