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

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Wed Dec 30 08:11:18 MSK 2009


Author: bigote
Date: 2009-12-30 08:11:18 +0300 (Wed, 30 Dec 2009)
New Revision: 262

Modified:
   trunk/plugins/reminder/reminder.tcl
Log:
Added saving reminders to file (on setting a new reminder and on deleting one).
TODO: Add saving reminders when closing Tkabber.
TODO: Add restoring them after Tkabber is started.


Modified: trunk/plugins/reminder/reminder.tcl
===================================================================
--- trunk/plugins/reminder/reminder.tcl	2009-12-29 22:01:03 UTC (rev 261)
+++ trunk/plugins/reminder/reminder.tcl	2009-12-30 05:11:18 UTC (rev 262)
@@ -1,4 +1,4 @@
-# "Reminder" plugin for Tkabber. 2009-12-29 v. 0.6
+# "Reminder" plugin for Tkabber. 2009-12-30 v. 0.7
 # 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,8 @@
 # either take a look at reminder::help procedure here.
 #####################################
 #
-# TODO: Think about saving and restoring long term reminders.
+# TODO: Add restoring long term reminders (their saving works already).
+# TODO: Add reminders saving on closing Tkabber.
 # TODO: Add some sound reminder notification.
 # TODO: Birthday reminding isn't ready yet, at all.
 #
@@ -36,6 +37,11 @@
         [::msgcat::mc "How many days before a birthday\
         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
 }
 
 proc reminder::load {} {
@@ -119,6 +125,9 @@
             lappend listrem_item $rid $exp_date "$message"
             lappend rem_list $listrem_item
             [namespace current]::list_reminders $chatid
+            if {$timeout > 300} {
+                [namespace current]::save_rem
+            }
         }
         /delrem {
             if {[info exists rem_list] && [llength $rem_list] > 0} {
@@ -136,6 +145,7 @@
             } else {
                 return stop
             }
+            [namespace current]::save_rem
         }
     }
     return stop
@@ -151,6 +161,7 @@
     if {[winfo exists $w]} {
         destroy $w
     }
+    set msg [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]
@@ -276,8 +287,67 @@
         \n\n/listrems\
         \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\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).\
+        \nThe list is supposed to be restored from the file after Tkabber's start.\
+        \nHowever, this part doesn't work yet ;-)"
     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 {} {
+    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 {
+puts \"$rem\"
+        lappend to_save \n$rem
+puts $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 {} {
+    variable rem_list
+    variable options
+    set log $options(reminders_file)
+puts $log
+    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]
+            foreach i $list {
+                if {$i != ""} {
+                    lappend rem_list [string trim $i]
+                }
+            }
+puts $rem_list
+        }
+    } else {
+        return
+    }
+}
+
+hook::add postload_hook reminder::load_rem 100



More information about the Tkabber-dev mailing list