kuuko pushed a commit to branch master.

http://git.enlightenment.org/apps/epour.git/commit/?id=7bfc3adcf15bf2f8d3ba969d572a3224a0828dc4

commit 7bfc3adcf15bf2f8d3ba969d572a3224a0828dc4
Author: Kai Huuhko <[email protected]>
Date:   Sun Jul 6 15:27:25 2014 +0300

    Add global setting for moving finished torrents
---
 epour/Epour.py           |  4 ++
 epour/gui/Preferences.py | 96 ++++++++++++++++++++++++++++++++++--------------
 epour/session.py         | 12 ++++++
 3 files changed, 85 insertions(+), 27 deletions(-)

diff --git a/epour/Epour.py b/epour/Epour.py
index e88b910..c886e00 100644
--- a/epour/Epour.py
+++ b/epour/Epour.py
@@ -130,6 +130,10 @@ class Epour(object):
             "storage_path": os.path.expanduser(
                 os.path.join("~", "Downloads")
             ),
+            "move_completed_path": os.path.expanduser(
+                os.path.join("~", "Downloads")
+            ),
+            "move_completed_enabled": str(False),
             "confirm_exit": str(False),
             "dialog_add_dbus": str(True),
             "delete_original": str(False),
diff --git a/epour/gui/Preferences.py b/epour/gui/Preferences.py
index b763105..f1a39c9 100644
--- a/epour/gui/Preferences.py
+++ b/epour/gui/Preferences.py
@@ -102,7 +102,42 @@ class PreferencesGeneral(PreferencesDialog):
         limits = Limits(self, session)
         ports = ListenPorts(self, session)
         pe = EncryptionSettings(self, session)
-        dlsel = DataStorageSelector(self, conf)
+        save_path_sel = StorageSelector(
+            self,
+            "Storage path",
+            lambda: conf.get("Settings", "storage_path"),
+            lambda x: conf.set("Settings", "storage_path", x),
+            size_hint_weight=EXPAND_HORIZ, size_hint_align=FILL_HORIZ
+            )
+
+        completed_sel_enabled = Check(
+            self, size_hint_align=ALIGN_LEFT, text="Move completed torrents",
+            )
+        completed_sel_enabled.state = conf.getboolean(
+            "Settings", "move_completed_enabled"
+            )
+
+        completed_sel = StorageSelector(
+            self,
+            "Completed torrents",
+            lambda: conf.get("Settings", "move_completed_path"),
+            lambda x: conf.set("Settings", "move_completed_path", x),
+            size_hint_weight=EXPAND_HORIZ, size_hint_align=FILL_HORIZ
+            )
+
+        def completed_sel_enabled_cb(chk, box, completed_sel):
+            enable = chk.state
+            conf.set("Settings", "move_completed_enabled", str(enable))
+            if enable:
+                box.pack_after(completed_sel, chk)
+                completed_sel.show()
+            else:
+                completed_sel.hide()
+                box.unpack(completed_sel)
+
+        completed_sel_enabled.callback_changed_add(
+            completed_sel_enabled_cb, self.box, completed_sel
+            )
 
         pad = Rectangle(self.evas)
         pad.color = 0, 0, 0, 0
@@ -138,33 +173,53 @@ class PreferencesGeneral(PreferencesDialog):
         sep2 = Separator(self)
         sep2.horizontal = True
 
-        for w in ports, limits, dlsel, pe, pad, sep1, chk1, chk2, chk3, sep2:
+        for w in (
+            ports, limits, save_path_sel, completed_sel_enabled,
+            pe, pad, sep1, chk1, chk2, chk3, sep2
+                ):
             w.show()
             self.box.pack_end(w)
 
+        if conf.getboolean("Settings", "move_completed_enabled"):
+            print("DRUUU DAT")
+            self.box.pack_after(completed_sel, completed_sel_enabled)
+            completed_sel.show()
 
-class DataStorageSelector(Frame):
 
-    def __init__(self, parent, conf):
-        Frame.__init__(self, parent)
+class StorageSelector(Frame):
 
-        self.size_hint_align = FILL_HORIZ
-        self.size_hint_weight = EXPAND_HORIZ
-        self.text = "Data storage"
+    def __init__(self, parent, title, read, write, *args, **kwargs):
+        Frame.__init__(self, parent, *args, **kwargs)
 
-        self.conf = conf
+        self.text = title
 
         b = Box(parent)
 
-        lbl = self.path_lbl = Label(parent)
-        lbl.text = conf.get("Settings", "storage_path")
+        lbl = Label(parent)
+        lbl.text = read()
 
-        self.dlsel = dlsel = FsButton(
+        dlsel = FsButton(
             self, size_hint_align=FILL_HORIZ, inwin_mode=False,
             text="Change path", folder_only=True, expandable=False
             )
-        dlsel.path = conf.get("Settings", "storage_path")
-        dlsel.callback_file_chosen_add(self.save_dlpath)
+        dlsel.path = read()
+
+        def save_dlpath(fs, path):
+            if not path:
+                return
+
+            if not os.path.exists(path):
+                Error(
+                    self,
+                    "Invalid path",
+                    "You have selected an invalid storage path."
+                    )
+                return
+
+            lbl.text = path
+            write(path)
+
+        dlsel.callback_file_chosen_add(save_dlpath)
 
         for w in lbl, dlsel:
             w.show()
@@ -173,19 +228,6 @@ class DataStorageSelector(Frame):
         b.show()
         self.content = b
 
-    def save_dlpath(self, fs, path):
-        if not path:
-            return
-
-        if not os.path.exists(self.dlsel.path):
-            Error(
-                self, "Invalid storage path",
-                "You have selected an invalid data storage path for torrents.")
-            return
-
-        self.path_lbl.text = path
-        self.conf.set("Settings", "storage_path", self.dlsel.path)
-
 
 class ListenPorts(Frame):
 
diff --git a/epour/session.py b/epour/session.py
index d6e9f81..ca822d6 100644
--- a/epour/session.py
+++ b/epour/session.py
@@ -86,6 +86,17 @@ class Session(lt.session):
         self.alert_manager.callback_add(
             "metadata_received_alert", self._metadata_received_cb)
 
+        def torrent_finished_move_cb(a):
+            h = a.handle
+            if conf.getboolean("Settings", "move_completed_enabled"):
+                path = conf.get("Settings", "move_completed_path")
+                if h.save_path() == path:
+                    return
+                h.move_storage(path)
+
+        self.alert_manager.callback_add(
+            "torrent_finished_alert", torrent_finished_move_cb)
+
     def _add_torrent_cb(self, a):
         e = a.error
         if e.value() > 0:
@@ -204,6 +215,7 @@ class Session(lt.session):
                 t = {}
                 t["info"] = lt.bdecode(t_info.metadata())
                 t_dict["ti"] = lt.bencode(t)
+                t_dict["save_path"] = h.save_path()
                 resume_data = lt.bencode(h.write_resume_data())
                 t_dict["resume_data"] = resume_data
 

-- 


Reply via email to