diff --git a/file_sharing/database.py b/file_sharing/database.py index 8bb4bbe..0c2e950 100644 --- a/file_sharing/database.py +++ b/file_sharing/database.py @@ -9,24 +9,24 @@ class FilesharingDatabase: path_l = os.path.split(plugin.config.FILE_PATH) path = os.path.join(path_l[0], 'shared_files.db') db_exist = os.path.exists(path) - print path self.conn = sqlite3.connect(path) # Enable foreign keys contraints self.conn.cursor().execute("pragma foreign_keys = on") if not db_exist: self.create_database() - # NOTE: Make sure we are getting and setting the requester without its resource + # NOTE: Make sure we are getting and setting the requester without its + # resource def create_database(self): c = self.conn.cursor() # Create tables c.execute("CREATE TABLE permissions" + - "(fid integer REFERENCES files(fid) ON DELETE CASCADE, " + - "account text, requester text)") + "(fid integer REFERENCES files(fid) ON DELETE CASCADE, " + + "account text, requester text)") c.execute("CREATE TABLE files" + - "(fid INTEGER PRIMARY KEY AUTOINCREMENT," + - " file_path text, relative_path text, hash_sha1 text," + - "size numeric, description text, mod_date text, is_dir boolean)") + "(fid INTEGER PRIMARY KEY AUTOINCREMENT," + + " file_path text, relative_path text, hash_sha1 text," + + "size numeric, description text, mod_date text, is_dir boolean)") # Save (commit) the changes self.conn.commit() c.close() @@ -34,10 +34,10 @@ class FilesharingDatabase: def get_toplevel_files(self, account, requester): c = self.conn.cursor() data = (account, requester) - c.execute("SELECT relative_path, hash_sha1, size, description, mod_date," + - " is_dir FROM (files JOIN permissions ON" + - " files.fid=permissions.fid) WHERE account=? AND requester=?" + - " AND relative_path NOT LIKE '%/%'", data) + c.execute("SELECT relative_path, hash_sha1, size, description, " + + "mod_date, is_dir FROM (files JOIN permissions ON" + + " files.fid=permissions.fid) WHERE account=? AND requester=?" + + " AND relative_path NOT LIKE '%/%'", data) result = c.fetchall() c.close() return result @@ -45,10 +45,10 @@ class FilesharingDatabase: def get_files_from_dir(self, account, requester, dir_): c = self.conn.cursor() data = (account, requester, dir_ + '/%') - c.execute("SELECT relative_path, hash_sha1, size, description, mod_date," + - " is_dir FROM (files JOIN permissions ON" + - " files.fid=permissions.fid) WHERE account=? AND requester=?" + - " AND relative_path LIKE ?", data) + c.execute("SELECT relative_path, hash_sha1, size, description, " + + "mod_date, is_dir FROM (files JOIN permissions ON" + + " files.fid=permissions.fid) WHERE account=? AND requester=?" + + " AND relative_path LIKE ?", data) result = c.fetchall() c.close() fresult = [] @@ -70,9 +70,9 @@ class FilesharingDatabase: """ c = self.conn.cursor() data = (account, requester) - c.execute("SELECT relative_path, hash_sha1, size, description, mod_date," + - " is_dir FROM (files JOIN permissions ON" + - " files.fid=permissions.fid) WHERE account=? AND requester=?", data) + c.execute("SELECT relative_path, hash_sha1, size, description, " + + "mod_date, is_dir FROM (files JOIN permissions ON" + + " files.fid=permissions.fid) WHERE account=? AND requester=?", data) result = c.fetchall() c.close() return result @@ -81,16 +81,16 @@ class FilesharingDatabase: c = self.conn.cursor() if hash_: data = (account, requester, hash_) - sql = "SELECT relative_path, hash_sha1, size, description, mod_date," + \ - " file_path FROM (files JOIN permissions ON" + \ - " files.fid=permissions.fid) WHERE account=? AND requester=?" + \ - " AND hash_sha1=?" + sql = "SELECT relative_path, hash_sha1, size, description, " + \ + "mod_date, file_path FROM (files JOIN permissions ON" + \ + " files.fid=permissions.fid) WHERE account=? AND requester=?" +\ + " AND hash_sha1=?" else: data = (account, requester, name) - sql = "SELECT relative_path, hash_sha1, size, description, mod_date," + \ - " file_path FROM (files JOIN permissions ON" + \ - " files.fid=permissions.fid) WHERE account=? AND requester=?" + \ - " AND relative_path=?" + sql = "SELECT relative_path, hash_sha1, size, description, " + \ + "mod_date, file_path FROM (files JOIN permissions ON" + \ + " files.fid=permissions.fid) WHERE account=? AND requester=?" +\ + " AND relative_path=?" c.execute(sql, data) result = c.fetchall() c.close() @@ -118,9 +118,8 @@ class FilesharingDatabase: requester = gajim.get_jid_without_resource(requester) c = self.conn.cursor() c.execute("INSERT INTO files (file_path, " + - "relative_path, hash_sha1, size, description, mod_date, " + - " is_dir) VALUES (?,?,?,?,?,?,?)", - file_) + "relative_path, hash_sha1, size, description, mod_date, " + + " is_dir) VALUES (?,?,?,?,?,?,?)", file_) fid = c.lastrowid permission_data = (fid, account, requester) c.execute("INSERT INTO permissions VALUES (?,?,?)", permission_data) @@ -132,14 +131,14 @@ class FilesharingDatabase: c = self.conn.cursor() data = (account, requester, file_[1]) c.execute("SELECT * FROM (files JOIN permissions ON" + - " files.fid=permissions.fid) WHERE account=? AND requester=?" + - " AND relative_path=? ", data) + " files.fid=permissions.fid) WHERE account=? AND requester=?" + + " AND relative_path=? ", data) result = c.fetchall() if file_[2] != '': data = (account, requester, file_[2]) c.execute("SELECT * FROM (files JOIN permissions ON" + - " files.fid=permissions.fid) WHERE account=? AND requester=?" + - " AND hash_sha1=?)", data) + " files.fid=permissions.fid) WHERE account=? AND requester=?" + + " AND hash_sha1=?)", data) result.extend(c.fetchall()) if len(result) > 0: raise Exception('Duplicated entry') @@ -156,9 +155,9 @@ class FilesharingDatabase: c = self.conn.cursor() data = (account, requester, dir_, dir_ + '/%') sql = "DELETE FROM files WHERE fid IN " + \ - " (SELECT files.fid FROM files, permissions WHERE" + \ - " files.fid=permissions.fid AND account=?"+ \ - " AND requester=? AND (relative_path=? OR relative_path LIKE ?))" + " (SELECT files.fid FROM files, permissions WHERE" + \ + " files.fid=permissions.fid AND account=?"+ \ + " AND requester=? AND (relative_path=? OR relative_path LIKE ?))" c.execute(sql, data) self.conn.commit() c.close() @@ -167,8 +166,8 @@ class FilesharingDatabase: c = self.conn.cursor() data = (account, requester, relative_path) c.execute("SELECT files.fid, is_dir FROM (files JOIN permissions ON" + - " files.fid=permissions.fid) WHERE account=? AND requester=? AND " + - "relative_path=? ", data) + " files.fid=permissions.fid) WHERE account=? AND requester=? AND " + + "relative_path=? ", data) result = c.fetchone() c.close() if result[1] == 0: @@ -180,7 +179,7 @@ class FilesharingDatabase: c = self.conn.cursor() data = (account, requester) sql = "DELETE FROM files WHERE fid IN (SELECT fid FROM permissions" + \ - " WHERE account=? AND requester=?)" + " WHERE account=? AND requester=?)" c.execute(sql, data) self.conn.commit() c.close() @@ -196,5 +195,5 @@ if __name__ == "__main__": conn = sqlite3.connect(path) # Enable foreign keys contraints conn.cursor().execute("pragma foreign_keys = on") - create_database() + FilesharingDatabase.create_database() doctest.testmod() diff --git a/file_sharing/fileshare_window.py b/file_sharing/fileshare_window.py index 1aa9fd2..6c479f9 100644 --- a/file_sharing/fileshare_window.py +++ b/file_sharing/fileshare_window.py @@ -137,10 +137,10 @@ class FileShareWindow(gtk.Window): def add_file(self, widget): dialog = gtk.FileChooserDialog('Add file to be shared', self, - gtk.FILE_CHOOSER_ACTION_OPEN, - (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, - gtk.STOCK_OPEN, gtk.RESPONSE_OK) - ) + gtk.FILE_CHOOSER_ACTION_OPEN, + (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, + gtk.RESPONSE_OK) + ) dialog.set_select_multiple(True) response = dialog.run() if response == gtk.RESPONSE_OK: @@ -150,10 +150,10 @@ class FileShareWindow(gtk.Window): def add_directory(self, widget): dialog = gtk.FileChooserDialog('Add directory to be shared', self, - gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, - (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, - gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT) - ) + gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, + (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, + gtk.RESPONSE_ACCEPT) + ) response = dialog.run() if response == gtk.RESPONSE_ACCEPT: file_list = [] @@ -171,17 +171,11 @@ class FileShareWindow(gtk.Window): dirpath = f.split('/') if len(dirpath) == 1: # Top level file, it doesnt have parent, add it right away - fref[dirpath[0]] = treestore.insert(root, - 0, - (dirpath[0],) - ) + fref[dirpath[0]] = treestore.insert(root, 0, (dirpath[0],)) else: for dir_ in dirpath: if tail + dir_ not in fref: - fref[tail + dir_] = treestore.append( - parent, - (dir_,) - ) + fref[tail + dir_] = treestore.append(parent, (dir_,)) parent = fref[tail + dir_] tail = tail + dir_ + '/' return fref @@ -191,7 +185,7 @@ class FileShareWindow(gtk.Window): import time, datetime ts = time.gmtime(epoch) dt = datetime.datetime(ts.tm_year, ts.tm_mon, ts.tm_mday, ts.tm_hour, - ts.tm_min, ts.tm_sec) + ts.tm_min, ts.tm_sec) return dt.isoformat() def add_items_tvcontacts(self, file_list, parentdir = None, parent = None): @@ -218,7 +212,8 @@ class FileShareWindow(gtk.Window): file_ = (f, relative_name, '', size, '', mod_date, is_dir) requester = self.cbb_contacts.get_active_text() try: - fid = self.plugin.database.add_file(self.account, requester, file_) + fid = self.plugin.database.add_file(self.account, requester, + file_) except Exception, e: if e == 'Duplicated entry': print 'Error: ' + e @@ -238,11 +233,10 @@ class FileShareWindow(gtk.Window): contacts = gajim.contacts.get_contacts(self.account, jid) for con in contacts: if con.show in ('offline', 'error') and not \ - con.supports(fshare_protocol.NS_FILE_SHARING): + con.supports(fshare_protocol.NS_FILE_SHARING): break cjid = con.get_full_jid() - r = self.ts_search.insert(None, 0, - (cjid, )) + r = self.ts_search.insert(None, 0, (cjid, )) self.browse_jid[cjid] = r pro = fshare.FileSharePlugin.prohandler[self.account] # Request list of files from peer @@ -255,17 +249,15 @@ class FileShareWindow(gtk.Window): self.cbb_contacts.grab_focus() for c in gajim.contacts.iter_contacts(self.account): jid = gajim.get_jid_without_resource(c.get_full_jid()) - r = self.ts_contacts.insert(None, len(self.ts_contacts), - (jid, )) + r = self.ts_contacts.insert(None, len(self.ts_contacts), (jid, )) if c.get_full_jid() == contact.get_full_jid(): self.cbb_contacts.set_active_iter(r) self.contacts_rows.append(r) self.manage_vbox2.set_sensitive(True) self.bt_remove.set_sensitive(False) self.add_file_list(self.plugin.database.get_files_name(self.account, - gajim.get_jid_without_resource(contact.get_full_jid())), - self.ts_files - ) + gajim.get_jid_without_resource(contact.get_full_jid())), + self.ts_files) def delete_event(self, widget, data=None): fshare.FileSharePlugin.filesharewindow = {} @@ -280,10 +272,8 @@ class FileShareWindow(gtk.Window): # If the contact in the comboboxentry is include inside of the # combobox if contact == self.ts_contacts.get_value(i, 0): - self.add_file_list(self.plugin.database.get_files_name(self.account, - contact), - self.ts_files - ) + self.add_file_list(self.plugin.database.get_files_name( + self.account, contact), self.ts_files) self.manage_vbox2.set_sensitive(True) self.bt_remove.set_sensitive(False) break @@ -331,8 +321,7 @@ class FileShareWindow(gtk.Window): pro = fshare.FileSharePlugin.prohandler[self.account] contact = self.get_contact_from_iter(self.ts_search, i) contact = gajim.contacts.get_contact_with_highest_priority( - self.account, - contact ) + self.account, contact) stanza = pro.request(contact.get_full_jid(), name, isFile=False) if pro.conn.connection: pro.conn.connection.send(stanza) @@ -371,7 +360,8 @@ class FileShareWindow(gtk.Window): file_info = self.brw_file_info[path] fjid = self.get_contact_from_iter(tree, row) # Request the file - file_path = os.path.join(self.plugin.config['incoming_dir'], file_info[0]) + file_path = os.path.join(self.plugin.config['incoming_dir'], + file_info[0]) sid = helpers.get_random_string_16() new_file_props = FilesProp.getNewFileProp(self.account, sid) new_file_props.file_name = file_path @@ -383,8 +373,7 @@ class FileShareWindow(gtk.Window): new_file_props.hash_ = None if file_info[3] == '' else file_info[3] new_file_props.type_ = 'r' tsid = gajim.connections[self.account].start_file_transfer(fjid, - new_file_props, - True) + new_file_props, True) new_file_props.transport_sid = tsid ft_window = gajim.interface.instances['file_transfers'] contact = gajim.contacts.get_contact_from_full_jid(self.account, fjid) @@ -392,9 +381,9 @@ class FileShareWindow(gtk.Window): def on_bt_sel_dir_pref_clicked(self, widget, data=None): chooser = gtk.FileChooserDialog(title='Incoming files directory', - action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, - buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL, - gtk.STOCK_OPEN,gtk.RESPONSE_OK)) + action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, + buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, + gtk.RESPONSE_OK)) response = chooser.run() if response == gtk.RESPONSE_OK: file_name = chooser.get_filename() @@ -407,4 +396,3 @@ if __name__ == "__main__": f = FileShareWindow(None) f.show() gtk.main() - diff --git a/file_sharing/fshare.py b/file_sharing/fshare.py index 9584ee9..4b6241d 100644 --- a/file_sharing/fshare.py +++ b/file_sharing/fshare.py @@ -25,8 +25,8 @@ class FileSharePlugin(GajimPlugin): @log_calls('FileSharePlugin') def init(self): self.activated = False - self.description = _('This plugin allows you to share folders'+ - ' with a peer using jingle file transfer.') + self.description = _('This plugin allows you to share folders' + ' with a peer using jingle file transfer.') self.config_dialog = None home_path = os.path.expanduser('~/') self.config_default_values = {'incoming_dir': (home_path, '')} @@ -105,8 +105,8 @@ class FileSharePlugin(GajimPlugin): submenu.attach(msf, 0, 1, 1, 2) submenu.attach(enable_fs, 0, 1, 2, 3) if gajim.account_is_disconnected(account) or \ - contact.show in ('offline', 'error') or not \ - contact.supports(fshare_protocol.NS_FILE_SHARING): + contact.show in ('offline', 'error') or not \ + contact.supports(fshare_protocol.NS_FILE_SHARING): bf.set_sensitive(False) submenu.show() bf.show() @@ -137,7 +137,8 @@ class FileSharePlugin(GajimPlugin): def __get_fsw_instance(self, account): # Makes sure we only have one instance of the window per account if account not in FileSharePlugin.filesharewindow: - FileSharePlugin.filesharewindow[account] = fsw = FileShareWindow(self) + FileSharePlugin.filesharewindow[account] = fsw = FileShareWindow( + self) FileSharePlugin.prohandler[account].set_window(fsw) return FileSharePlugin.filesharewindow[account]