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

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Sat Jan 2 13:17:46 MSK 2010


Author: bigote
Date: 2010-01-02 13:17:46 +0300 (Sat, 02 Jan 2010)
New Revision: 264

Modified:
   trunk/plugins/reminder/reminder.tcl
Log:
Rewritten storing and restoing reminders via custom::defvar.
All remindres are stored in custom.tcl now.


Modified: trunk/plugins/reminder/reminder.tcl
===================================================================
--- trunk/plugins/reminder/reminder.tcl	2010-01-01 18:18:29 UTC (rev 263)
+++ trunk/plugins/reminder/reminder.tcl	2010-01-02 10:17:46 UTC (rev 264)
@@ -1,4 +1,4 @@
-# "Reminder" plugin for Tkabber. 2010-01-01 v. 0.8
+# "Reminder" plugin for Tkabber. 2010-01-02 v. 0.8.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.
@@ -8,7 +8,6 @@
 # either take a look at reminder::help procedure here.
 #####################################
 #
-# TODO: Rewrite storing and restoing reminders via custom::defvar
 # TODO: Add some sound reminder notification.
 # TODO: Birthday reminding isn't ready yet, at all.
 #
@@ -37,17 +36,20 @@
         you would like to be reminded."] \
         -type string -group Reminder
 
-    custom::defvar options(reminders_file) [file join $::configdir reminders.txt] \
-        [::msgcat::mc "Path to reminders file.\
-        If empty, you won't be able to save and restore long-term reminders."] \
-        -type file -group Reminder
+    custom::defvar stored_rem_list {} \
+        "List of reminders to store" \
+        -type string -group Hidden
+
 }
 
 proc reminder::load {} {
     variable rem_list
-    variable rem_number
+    variable rem_list2save
     variable winid 0
-    [namespace current]::restore_reminders
+    trace variable [namespace current]::rem_list2save w \
+    [namespace current]::save_reminders
+    hook::add finload_hook \
+    [namespace current]::restore_reminders 71
     hook::add generate_completions_hook \
     [namespace current]::reminder_compls
     hook::add chat_send_message_hook \
@@ -56,7 +58,7 @@
 
 proc reminder::unload {} {
     variable rem_list
-    variable rem_number
+    variable rem_list2save
     variable winid
     hook::remove generate_completions_hook \
     [namespace current]::reminder_compls
@@ -64,7 +66,7 @@
     [namespace current]::handle_commands 18
     catch {unset winid}
     catch {unset rem_list}
-    catch {unset rem_number}
+    catch {unset rem_list2save}
 }
 
 proc reminder::reminder_compls {chatid compsvar wordstart line} {
@@ -137,7 +139,6 @@
             } else {
                 return stop
             }
-            [namespace current]::save_reminders
         }
     }
     return stop
@@ -145,15 +146,15 @@
 
 proc reminder::set_reminder {timeout message} {
     variable rem_list
+    variable rem_list2save
+
     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
-    }
+    set rem_list2save $rem_list
 }
 
 proc reminder::show_reminder {msg} {
@@ -168,8 +169,7 @@
         -modal none -separator 1 -anchor e -default 0 -parent .
         set f [$w getframe]
         $w add -text [::msgcat::mc "OK"] -command "destroy $w; \
-        [namespace current]::clean_rem_list; \
-        [namespace current]::save_reminders"
+        [namespace current]::clean_rem_list"
         label $w.lsure -padx 10 -pady 5 -text [::msgcat::mc $msg]
         pack $w.lsure -expand yes -fill both
         $w draw
@@ -177,7 +177,7 @@
 
 proc reminder::list_reminders {chatid} {
     variable rem_list
-    variable rem_number
+    variable r_number
     set group [chat::get_jid $chatid]
     chat::add_message $chatid $group info "List of current reminders:" {}
     if {![info exists rem_list]} {
@@ -185,16 +185,17 @@
         return stop
     }
     [namespace current]::clean_rem_list
+    set r_number 1
     foreach lri $rem_list {
-        set msg [list $rem_number\tExpires:\ [clock format [lindex $lri 1]] [lindex $lri 2]]
+        set msg [list $r_number\tExpires:\ [clock format [lindex $lri 1]] [lindex $lri 2]]
         chat::add_message $chatid $group info "[join $msg]" {}
+        incr r_number
     }
-    [namespace current]::save_reminders
 }
 
 proc reminder::clean_rem_list {} {
+    variable rem_list2save
     variable rem_list
-    variable rem_number
     set r_number 1
     set rl_1 $rem_list
 # Checking for expired reminders and removing them, if any.
@@ -206,7 +207,7 @@
         }
     }
     set rem_list $rl_1
-    set rem_number $r_number
+    set rem_list2save $rem_list
 }
 
 proc reminder::parse_timeout {to chatid} {
@@ -303,70 +304,42 @@
         \n\tThis will show you a list of all running reminders you have set.\
         \n\n/delrem 1\
         \n\tThis will delete reminder number one (from a list obtained by /listrems).\
-        \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).\
+        \n\nAll reminders are stored in custom.tcl automatically\
+        \nafter having set a new reminder and after deleting one.\
         \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_reminders {} {
-    variable options
-    variable rem_list
-    set log $options(reminders_file)
-    if { $log eq "" } {
-        return
-    }
-    if {[catch {open $log w} fd]} {
-        debugmsg reminder \
-        "ERROR: cannot open reminders file."
-        return
-    }
-    foreach rem $rem_list {
-        lappend to_save \n$rem
-    }
-    if {[info exists to_save]} {
-        puts $fd "[join $to_save]"
-        close $fd
-    }
+proc reminder::save_reminders args {
+    variable rem_list2save
+    variable stored_rem_list
+
+    set stored_rem_list $rem_list2save
 }
 
-proc reminder::restore_reminders {} {
-    variable options
+
+proc reminder::restore_reminders args {
     variable rem_list
-    set log $options(reminders_file)
-    if { $log eq "" } {
-        return
-    }
-    if {[file exists $log]} {
-        if {[catch {open $log r} fd]} {
-            debugmsg reminder \
-            "ERROR: cannot open reminders file."
-            return
-        } else {
-            set list [split [read $fd] \n]
-            close $fd
-            foreach i $list {
-                if {$i != ""} {
+    variable stored_rem_list
+
+    set list $stored_rem_list
+    foreach i $list {
+        if {$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]
+            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 {$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]
-                    }
-                }
+                lappend rem_list $i
+                set msg "Old reminder. Expired: [clock format $old_time]\n$msg"
+                after idle [namespace current]::show_reminder [list $msg]
             }
         }
-    } else {
-        return
     }
 }



More information about the Tkabber-dev mailing list