[hamster-applet] in case if suggestion comes from outside, we don't automatically add it to our activities to avoid p



commit 2daa505d427dbd6230c693d910883f4e9df3774e
Author: Toms Bauģis <toms baugis gmail com>
Date:   Sat May 15 11:18:21 2010 +0100

    in case if suggestion comes from outside, we don't automatically add it to our activities to avoid polluting autocomplete

 src/hamster/applet.py                |    8 ++++--
 src/hamster/client.py                |   19 +++++++++++------
 src/hamster/db.py                    |   37 ++++++++++++++++++---------------
 src/hamster/edit_activity.py         |   32 +++++++++++------------------
 src/hamster/storage.py               |   21 +++++++++++--------
 src/hamster/widgets/activityentry.py |   23 ++++++++++++++++++++-
 6 files changed, 83 insertions(+), 57 deletions(-)
---
diff --git a/src/hamster/applet.py b/src/hamster/applet.py
index d395e87..03e0ee9 100755
--- a/src/hamster/applet.py
+++ b/src/hamster/applet.py
@@ -614,7 +614,7 @@ class HamsterApplet(object):
 
                 activity = runtime.storage.get_activity_by_name(parsed_activity.activity_name,
                                                                 category_id,
-                                                                ressurect = False)
+                                                                resurrect = False)
                 if activity:
                     # we need dict below
                     activity = dict(name = activity['name'],
@@ -690,11 +690,13 @@ class HamsterApplet(object):
         self.get_widget("switch_activity").set_sensitive(widget.get_text() != "")
 
     def on_switch_activity_clicked(self, widget):
-        if not self.new_name.get_text():
+        activity_name, temporary = self.new_name.get_value()
+        if not activity_name:
             return False
 
         runtime.storage.add_fact(self.new_name.get_text().decode("utf8", "replace"),
-                                 self.new_tags.get_text().decode("utf8", "replace"))
+                                 self.new_tags.get_text().decode("utf8", "replace"),
+                                 temporary = temporary)
         self.new_name.set_text("")
         self.new_tags.set_text("")
         self.__show_toggle(False)
diff --git a/src/hamster/client.py b/src/hamster/client.py
index 11daec9..4a0eef4 100644
--- a/src/hamster/client.py
+++ b/src/hamster/client.py
@@ -141,7 +141,8 @@ class Storage(gobject.GObject):
         return from_dbus_fact(self.conn.GetFact(id))
 
     def add_fact(self, activity_name, tags = '', start_time = None, end_time = None,
-                                      category_name = None, description = None):
+                                      category_name = None, description = None,
+                                      temporary = False):
         """Add fact. activity name can use `[-]start_time[-end_time] activity category, description #tag1 #tag2`
         syntax, or params can be stated explicitly.
         Params, except for the start and end times will take precedence over
@@ -165,7 +166,7 @@ class Storage(gobject.GObject):
         category_name = category_name or ''
         description = description or ''
 
-        return self.conn.AddFact(activity_name, tags, start_time, end_time, category_name, description)
+        return self.conn.AddFact(activity_name, tags, start_time, end_time, category_name, description, temporary)
 
     def stop_tracking(self, end_time = None):
         """Stop tracking current activity. end_time can be passed in if the
@@ -177,7 +178,9 @@ class Storage(gobject.GObject):
         "delete fact from database"
         self.conn.RemoveFact(fact_id)
 
-    def update_fact(self, fact_id, activity_name, tags = None, start_time = None, end_time = None, category_name = None, description = None):
+    def update_fact(self, fact_id, activity_name, tags = None,
+                    start_time = None, end_time = None,
+                    category_name = None, description = None, temporary = False):
         """Update fact values. See add_fact for rules.
         Update is performed via remove/insert, so the
         fact_id after update should not be used anymore. Instead use the ID
@@ -198,7 +201,9 @@ class Storage(gobject.GObject):
             tags = ", ".join(tags)
         tags = tags or ''
 
-        return self.conn.UpdateFact(fact_id, activity_name, tags, start_time, end_time, category_name, description)
+        return self.conn.UpdateFact(fact_id, activity_name, tags,
+                                    start_time, end_time,
+                                    category_name, description, temporary)
 
 
     def get_activities(self, category_id = None):
@@ -211,13 +216,13 @@ class Storage(gobject.GObject):
         """returns category id by name"""
         return self.conn.GetCategoryId(category_name)
 
-    def get_activity_by_name(self, activity, category_id = None, ressurect = True):
+    def get_activity_by_name(self, activity, category_id = None, resurrect = True):
         """returns activity dict by name and optionally filtering by category.
            if activity is found but is marked as deleted, it will be resurrected
-           unless told otherise in the ressurect param
+           unless told otherise in the resurrect param
         """
         category_id = category_id or 0
-        return self.conn.GetActivityByName(activity, category_id, ressurect)
+        return self.conn.GetActivityByName(activity, category_id, resurrect)
 
     # category and activity manipulations (normally just via preferences)
     def remove_activity(self, id):
diff --git a/src/hamster/db.py b/src/hamster/db.py
index a1594ee..de6e130 100644
--- a/src/hamster/db.py
+++ b/src/hamster/db.py
@@ -127,7 +127,7 @@ class Storage(storage.Storage):
 
         changes = False
 
-        # check if any of tags needs ressurection
+        # check if any of tags needs resurrection
         set_complete = [str(tag["id"]) for tag in db_tags if tag["autocomplete"] == "false"]
         if set_complete:
             changes = True
@@ -250,7 +250,7 @@ class Storage(storage.Storage):
 
 
 
-    def __get_activity_by_name(self, name, category_id = None, ressurect = True):
+    def __get_activity_by_name(self, name, category_id = None, resurrect = True):
         """get most recent, preferably not deleted activity by it's name"""
 
         if category_id:
@@ -280,12 +280,9 @@ class Storage(storage.Storage):
             res = dict(res)
             res['deleted'] = res['deleted'] or False
 
-            # if the activity was marked as deleted, ressurect on first call
+            # if the activity was marked as deleted, resurrect on first call
             # and put in the unsorted category
-            if res['deleted'] and not ressurect:
-                return None
-
-            elif res['deleted']:
+            if res['deleted'] and resurrect:
                 update = """
                             UPDATE activities
                                SET deleted = null, category_id = -1
@@ -479,7 +476,8 @@ class Storage(storage.Storage):
 
 
     def __add_fact(self, activity_name, tags, start_time = None,
-                     end_time = None, category_name = None, description = None):
+                   end_time = None, category_name = None,
+                   description = None, temporary = False):
 
         activity = stuff.parse_activity_input(activity_name)
 
@@ -531,16 +529,16 @@ class Storage(storage.Storage):
             if not category_id:
                 category_id = self.__add_category(activity.category_name)
 
-        # try to find activity
+        # try to find activity, resurrect if not temporary
         activity_id = self.__get_activity_by_name(activity.activity_name,
-                                                  category_id)
+                                                  category_id,
+                                                  resurrect = not temporary)
         if not activity_id:
             activity_id = self.__add_activity(activity.activity_name,
-                                              category_id)
+                                              category_id, temporary)
         else:
             activity_id = activity_id['id']
 
-
         # if we are working on +/- current day - check the last_activity
         if (dt.datetime.now() - start_time <= dt.timedelta(days=1)):
 
@@ -829,7 +827,7 @@ class Storage(storage.Storage):
                       "update activities set activity_order = ? where id = ?"],
                       [(priority1, id2), (priority2, id1)])
 
-    def __add_activity(self, name, category_id = None):
+    def __add_activity(self, name, category_id = None, temporary = False):
         # first check that we don't have anything like that yet
         activity = self.__get_activity_by_name(name, category_id)
         if activity:
@@ -839,11 +837,16 @@ class Storage(storage.Storage):
         category_id = category_id or -1
         new_order = self.fetchone("select max(activity_order) + 1  from activities")[0] or 1
 
+        deleted = None
+        if temporary:
+            deleted = 1
+
+
         query = """
-                   INSERT INTO activities (name, category_id, activity_order)
-                        VALUES (?, ?, ?)
+                   INSERT INTO activities (name, category_id, activity_order, deleted)
+                        VALUES (?, ?, ?, ?)
         """
-        self.execute(query, (name, category_id, new_order))
+        self.execute(query, (name, category_id, new_order, deleted))
         return self.__last_insert_rowid()
 
     def __update_activity(self, id, name, category_id):
@@ -1087,7 +1090,7 @@ class Storage(storage.Storage):
             # previously deleted records are now unsorted ones
             # user will be able to mark them as deleted again, in which case
             # they won't appear in autocomplete, or in categories
-            # ressurection happens, when user enters the exact same name
+            # resurrection happens, when user enters the exact same name
             self.execute("""
                                CREATE TABLE activities_new (id integer primary key,
                                                             name varchar2(500),
diff --git a/src/hamster/edit_activity.py b/src/hamster/edit_activity.py
index 6141216..6985798 100644
--- a/src/hamster/edit_activity.py
+++ b/src/hamster/edit_activity.py
@@ -154,9 +154,6 @@ class CustomFactController:
 
 
     def figure_description(self):
-        activity = self.new_name.get_text().decode("utf-8")
-
-        # juggle with description - break into parts and then put together
         buf = self.get_widget('description').get_buffer()
         description = buf.get_text(buf.get_start_iter(), buf.get_end_iter(), 0)\
                          .decode("utf-8")
@@ -185,24 +182,17 @@ class CustomFactController:
             return None
 
     def on_save_button_clicked(self, button):
-        activity = self.new_name.get_text().decode("utf-8")
-        parsed = stuff.parse_activity_input(activity)
+        activity_name, temporary = self.new_name.get_value()
+        print activity_name, temporary
+        parsed = stuff.parse_activity_input(activity_name)
 
         if not parsed.activity_name:
             return False
 
-
-        # user might also type description in the activity name - strip it here
-        # and remember value
-        inline_description = None
-        if activity.find(",") != -1:
-            activity, inline_description  = activity.split(",", 1)
-            inline_description = inline_description.strip()
-
         # explicit takes precedence
-        description = self.figure_description() or inline_description
+        description = self.figure_description()
 
-        tags = self.new_tags.get_text().decode('utf8', 'replace')
+        tags = self.new_tags.get_text().decode('utf8')
 
         start_time = self._get_datetime("start")
 
@@ -213,19 +203,21 @@ class CustomFactController:
 
         if self.fact_id:
             runtime.storage.update_fact(self.fact_id,
-                                        parsed.activity_name,
+                                        activity_name,
                                         tags,
                                         start_time,
                                         end_time,
                                         parsed.category_name,
-                                        description)
+                                        description,
+                                        temporary = temporary)
         else:
-            runtime.storage.add_fact(parsed.activity_name,
+            runtime.storage.add_fact(activity_name,
                                      tags,
                                      start_time,
                                      end_time,
                                      parsed.category_name,
-                                     description)
+                                     description,
+                                     temporary = temporary)
 
         self.close_window()
 
@@ -266,7 +258,7 @@ class CustomFactController:
         self.get_widget("end_date_label").set_text(some_date.strftime("%x"))
 
     def validate_fields(self, widget = None):
-        activity_text = self.new_name.get_text().decode('utf8', 'replace')
+        activity_text, temporary = self.new_name.get_value()
         start_time = self._get_datetime("start")
 
         if self.get_widget("in_progress").get_active():
diff --git a/src/hamster/storage.py b/src/hamster/storage.py
index 74290d0..ea7c248 100644
--- a/src/hamster/storage.py
+++ b/src/hamster/storage.py
@@ -83,9 +83,9 @@ class Storage(dbus.service.Object):
 
     # facts
 
-    @dbus.service.method("org.gnome.Hamster", in_signature='ssiiss', out_signature='i')
+    @dbus.service.method("org.gnome.Hamster", in_signature='ssiissb', out_signature='i')
     def AddFact(self, activity_name, tags, start_time, end_time,
-                                      category_name = None, description = None):
+                category_name = None, description = None, temporary = False):
 
         if start_time:
             start_time = dt.datetime.utcfromtimestamp(start_time)
@@ -98,7 +98,7 @@ class Storage(dbus.service.Object):
             end_time = None
 
         self.start_transaction()
-        result = self.__add_fact(activity_name, tags, start_time, end_time, category_name, description)
+        result = self.__add_fact(activity_name, tags, start_time, end_time, category_name, description, temporary)
         self.end_transaction()
 
         if result:
@@ -115,8 +115,10 @@ class Storage(dbus.service.Object):
         return to_dbus_fact(fact)
 
 
-    @dbus.service.method("org.gnome.Hamster", in_signature='issiiss', out_signature='i')
-    def UpdateFact(self, fact_id, activity_name, tags, start_time, end_time, category_name = None, description = None):
+    @dbus.service.method("org.gnome.Hamster", in_signature='issiissb', out_signature='i')
+    def UpdateFact(self, fact_id, activity_name, tags,
+                   start_time, end_time,
+                   category_name = None, description = None, temporary = False):
         if start_time:
             start_time = dt.datetime.utcfromtimestamp(start_time)
         else:
@@ -127,10 +129,11 @@ class Storage(dbus.service.Object):
         else:
             end_time = None
 
-
         self.start_transaction()
         self.__remove_fact(fact_id)
-        result = self.__add_fact(activity_name, tags, start_time, end_time, category_name, description)
+        result = self.__add_fact(activity_name, tags, start_time, end_time,
+                                 category_name, description, temporary)
+
         self.end_transaction()
 
         if result:
@@ -304,11 +307,11 @@ class Storage(dbus.service.Object):
 
 
     @dbus.service.method("org.gnome.Hamster", in_signature='sib', out_signature='a{sv}')
-    def GetActivityByName(self, activity, category_id, ressurect = True):
+    def GetActivityByName(self, activity, category_id, resurrect = True):
         category_id = category_id or None
 
         if activity:
-            return dict(self.__get_activity_by_name(activity, category_id, ressurect))
+            return dict(self.__get_activity_by_name(activity, category_id, resurrect))
         else:
             return {}
 
diff --git a/src/hamster/widgets/activityentry.py b/src/hamster/widgets/activityentry.py
index 8ebf2e8..534b966 100644
--- a/src/hamster/widgets/activityentry.py
+++ b/src/hamster/widgets/activityentry.py
@@ -35,6 +35,7 @@ class ActivityEntry(gtk.Entry):
         gtk.Entry.__init__(self)
         self.news = False
         self.activities = None
+        self.external_activities = [] # suggestions from outer space
         self.categories = None
         self.filter = None
         self.max_results = 10 # limit popup size to 10 results
@@ -99,6 +100,24 @@ class ActivityEntry(gtk.Entry):
         self.show()
         self.populate_suggestions()
 
+    def get_value(self):
+        activity_name = self.get_text().decode("utf-8")
+        if not activity_name:
+            return None, False
+
+        # see if entered text matches something from the outer suggestions
+        # only consequence of if it does is that it will not attempt to
+        # ressurect the activity if it's deleted (hidden)
+        # thus avoiding polluting our local suggestions
+        external_names = set()
+        for activity in self.external_activities:
+            name = activity['name']
+            if activity['category']:
+                name = "%s %s" % name, activity['category']
+            external_names.add(name.lower())
+
+        return activity_name, activity_name in external_names
+
     def hide_popup(self):
         if self._parent_click_watcher and self.get_toplevel().handler_is_connected(self._parent_click_watcher):
             self.get_toplevel().disconnect(self._parent_click_watcher)
@@ -193,7 +212,9 @@ class ActivityEntry(gtk.Entry):
 
         # do not cache as ordering and available options change over time
         self.activities = runtime.storage.get_autocomplete_activities(input_activity.activity_name)
-        self.activities.extend(self.external.get_activities(input_activity.activity_name))
+        self.external_activities = self.external.get_activities(input_activity.activity_name)
+        self.activities.extend(self.external_activities)
+
         self.categories = self.categories or runtime.storage.get_categories()
 
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]