Commits

Damián Nohales committed 3cf2d6a

Checkbox for assume action on filename overwrite at downloads

Also, don't allow repeated downloads on the download list

Comments (0)

Files changed (3)

data/overwrite_dialog.ui

+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <requires lib="gtk+" version="2.24"/>
+  <!-- interface-naming-policy project-wide -->
+  <object class="GtkDialog" id="dialog">
+    <property name="width_request">600</property>
+    <property name="height_request">215</property>
+    <property name="can_focus">False</property>
+    <property name="border_width">5</property>
+    <property name="title" translatable="yes">Overwrite downloaded file?</property>
+    <property name="resizable">False</property>
+    <property name="type_hint">dialog</property>
+    <property name="has_resize_grip">False</property>
+    <child internal-child="vbox">
+      <object class="GtkVBox" id="dialog-vbox1">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="spacing">2</property>
+        <child>
+          <object class="GtkLabel" id="label">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="label" translatable="yes">The file '%s' already exists, maybe you are downloading a file that you downloaded before.
+
+What do you want to do?</property>
+            <property name="wrap">True</property>
+          </object>
+          <packing>
+            <property name="expand">True</property>
+            <property name="fill">True</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkAlignment" id="alignment1">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="left_padding">20</property>
+            <child>
+              <object class="GtkCheckButton" id="checkbutton">
+                <property name="label" translatable="yes">Always perform the selected action for the following conflicts</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">False</property>
+                <property name="use_action_appearance">False</property>
+                <property name="draw_indicator">True</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkHSeparator" id="hseparator1">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">2</property>
+          </packing>
+        </child>
+        <child internal-child="action_area">
+          <object class="GtkHButtonBox" id="dialog-action_area1">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="layout_style">center</property>
+            <child>
+              <object class="GtkButton" id="button1">
+                <property name="label" translatable="yes">Cancel download</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="has_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="use_action_appearance">False</property>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkButton" id="button2">
+                <property name="label" translatable="yes">Download and overwrite</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="use_action_appearance">False</property>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">1</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkButton" id="button3">
+                <property name="label" translatable="yes">Download renamed</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="use_action_appearance">False</property>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">2</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="pack_type">end</property>
+            <property name="position">3</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+    <action-widgets>
+      <action-widget response="0">button1</action-widget>
+      <action-widget response="1">button2</action-widget>
+      <action-widget response="2">button3</action-widget>
+    </action-widgets>
+  </object>
+</interface>

lib/DownloadList.py

         # song = Song(song.get_data())
         # The above line cause problems, I don't know why :( .
         
+        if self.find_song(song) != None:
+            raise Exception()
+        
         self.get_model().append([
             song,
             unicode(os.path.basename(song.get_filename()), errors = 'replace'),
         """
         Append song for download, if file exists, tries to continue the download
         """
-        self.__append_song_to_model(song);
-        song.start_download(False)
+        try:
+            self.__append_song_to_model(song);
+            song.start_download(False)
+        except Exception:
+            pass
         
     def append_song_restarting(self, song):
         """
         Append song for download, forcing to redownload the file
         """
-        self.__append_song_to_model(song);
-        song.start_download(True)
+        try:
+            self.__append_song_to_model(song);
+            song.start_download(True)
+        except Exception:
+            pass
         
     def create_view(self):
         column = gtk.TreeViewColumn("", gtk.CellRendererPixbuf(), stock_id = 5)
             search_thread = SearchThread(self, widget.get_text(), "Songs")
             search_thread.start()
 
-    def get_overwritten_filename(self, filename):
+    def get_overwritten_filename(self, filename, use_response = None):
         """
         Gets the file name depending on whether user want to overwrite the file,
         not overwrite it or not save it. Returns the same filename if user want
         to overwrite, the renamed filename or None if the user cancel Download.
         """
         if os.path.exists(filename) == True:
-            dialog = gtk.Dialog(_("Overwrite downloaded file?"),
-                         self.window,
-                         gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
-                         (_("Cancel download"), gtk.RESPONSE_NONE,
-                          _("Download and overwrite"), 0,
-                          _("Download renamed"), 1))
+            if use_response == None:
+                builder = gtk.Builder()
+                builder.set_translation_domain(env().APP)
+                builder.add_from_file('%s/data/overwrite_dialog.ui' % env().BASEPATH)
+                
+                dialog = builder.get_object("dialog")
+                label = builder.get_object("label")
+                checkbox = builder.get_object("checkbutton")
+                
+                label.set_text(label.get_text() % filename)
+                
+                response = dialog.run()
+                
+                checkbox_active = checkbox.get_active()
+                dialog.destroy()
+            else:
+                response = use_response
+                checkbox_active = True
+        else:
+            response = 1
+            checkbox_active = False
+            
+        if checkbox_active == True:
+            next_response = response
+        else:
+            next_response = None
 
-            label = gtk.Label(_("The file '%s' already exists, maybe you are downloading a file that you downloaded before.\n\nWhat do you want to do?") % filename)
-            label.set_line_wrap(True)
-            dialog.vbox.pack_start(label)
-            label.show()
-
-            dialog.set_default_response(gtk.RESPONSE_NONE)
-            dialog.set_resizable(False)
-            dialog.set_size_request(600, 175)
-            response = dialog.run()
-            dialog.destroy()
-        else:
-            response = 0
-
-        if response == 1:
+        if response == 2:
             i = 1
             newfilename = ""
             while i == 1 or os.path.exists(newfilename):
                 split = os.path.splitext(filename)
                 newfilename = split[0] + " (%d)" % i + split[1]
 
-            return newfilename
-        elif response == 0:
-            return filename
+            return (newfilename, next_response)
+        elif response == 1:
+            return (filename, next_response)
         else:
-            return None
+            return (None, next_response)
 
     def on_download_selected(self, widget, data = None):
         """
         Starts the download thread
         """
+        next_response = None
         select = self.result.get_selected_rows()
         for path in select:
             song = self.result.get_song(path)
-
-            filename = self.get_overwritten_filename(song.get_default_filename())
-
-            if filename != None:
-                song.set_filename(filename)
-                self.downloads.append_song_restarting(song)
+            
+            if self.downloads.find_song(song) == None:
+                dialog_response = self.get_overwritten_filename(song.get_default_filename(), next_response)
+                
+                filename = dialog_response[0]
+                next_response = dialog_response[1]
+    
+                if filename != None:
+                    song.set_filename(filename)
+                    self.downloads.append_song_restarting(song)
 
     def on_downloads_changed(self, widget, count):
         """