Răsfoiți Sursa

fixed problem with symlink
added symlink icon, 0 size
messagebox to prevent comment over-write (how can this happen?)
added stuff to the menu

Pat Beirne 9 ani în urmă
părinte
comite
c5c30b6433
1 a modificat fișierele cu 32 adăugiri și 10 ștergeri
  1. 32 10
      dirnotes

+ 32 - 10
dirnotes

@@ -63,7 +63,7 @@ class DataBase:
 	def log(self, fileName, comment):
 		''' TODO: convert filename to canonical '''
 		c = self.db.cursor()
-		s = os.stat(fileName)
+		s = os.lstat(fileName)
 		print "params: %s %s %d %s %s" % ((os.path.abspath(fileName), 
 				DataBase.epochToDb(s.st_mtime), s.st_size, comment, 
 				DataBase.epochToDb(time.time())))
@@ -113,12 +113,16 @@ def parse():
 
 
 class FileObj():
+	FILE_IS_DIR = -1
+	FILE_IS_LINK = -2
 	def __init__(self, fileName):
 		self.fileName = fileName
-		s = os.stat(fileName)
+		s = os.lstat(fileName)
 		self.date = DataBase.epochToDb(s.st_mtime)
 		if stat.S_ISDIR(s.st_mode):
-			self.size = -1
+			self.size = FileObj.FILE_IS_DIR
+		elif stat.S_ISLNK(s.st_mode):
+			self.size = FileObj.FILE_IS_LINK
 		else:
 			self.size = s.st_size
 		self.comment = ''
@@ -139,6 +143,7 @@ class FileObj():
 		# we need to move these cases out to a handler 
 		except Exception as e:
 			print("problem setting the comment on file %s" % (self.fileName,))
+			print("error "+e)
 			if os.access(self.fileName, os.W_OK)!=True:
 				print("you don't appear to have write permissions on this file")
 				# change the listbox background to yellow
@@ -153,6 +158,7 @@ class FileObj():
 
 # sortable TableWidgetItem, based on idea by Aledsandar
 # http://stackoverflow.com/questions/12673598/python-numerical-sorting-in-qtablewidget
+# NOTE: the QTableWidgetItem has setData() and data() which may allow data bonding
 class SortableTableWidgetItem(QTableWidgetItem):
 	def __init__(self, text, sortValue):
 		QTableWidgetItem.__init__(self, text, QTableWidgetItem.UserType)
@@ -236,9 +242,11 @@ class DirNotes(QMainWindow):
 		mf.addAction("Sort by size", self.sbs, "Ctrl+Z")
 		mf.addAction("Sort by comment", self.sbc, "Ctrl+.")
 		mf.addAction("Restore comment from database", self.restore_from_database, "Ctrl+R")
+		mf.addSeparator()
+		mf.addAction("Quit", self.close, "Ctrl+Q")
 
-		QShortcut(QKeySequence("Ctrl+Q"), self, self.close)	
-		self.setWindowTitle("DirNotes")
+		#~ QShortcut(QKeySequence("Ctrl+Q"), self, self.close)	
+		self.setWindowTitle("DirNotes   Alt-F for menu  Dir: "+self.curPath)
 		self.setMinimumSize(600,700)
 		lb.setFocus()
 	def closeEvent(self,e):
@@ -263,6 +271,7 @@ class DirNotes(QMainWindow):
 		small_font = QFont("",8)
 		dirIcon = QIcon.fromTheme('folder')
 		fileIcon = QIcon.fromTheme('text-x-generic')
+		linkIcon = QIcon.fromTheme('emblem-symbolic-link')
 		current, dirs, files = os.walk(self.curPath,followlinks=True).next()
 		dirs = map(lambda x:x+'/', dirs)
 		dirs.sort()
@@ -271,9 +280,12 @@ class DirNotes(QMainWindow):
 		d = dirs + files
 		self.lb.setRowCount(len(d))
 
+		#~ self.files = {}
 		self.files = []
+		# this is a list of all the file
 		for i in range(len(d)):
 			this_file = FileObj(current+'/'+d[i])
+			#~ self.files.update({this_file.getName(),this_file})
 			self.files = self.files + [this_file]
 			item = QTableWidgetItem(d[i])
 			item.setFlags(QtCore.Qt.ItemIsEnabled)
@@ -287,12 +299,15 @@ class DirNotes(QMainWindow):
 			da.setFlags(QtCore.Qt.ItemIsEnabled)
 			self.lb.setItem(i,1,da)
 			si = this_file.getSize()
-			if si>=0:
-				sa = SortableTableWidgetItem(str(si),si)
-				item.setIcon(fileIcon)
-			else:
+			if si==FileObj.FILE_IS_DIR:
 				sa = SortableTableWidgetItem('',0)
 				item.setIcon(dirIcon)
+			elif si==FileObj.FILE_IS_LINK:
+				sa = SortableTableWidgetItem('',0)
+				item.setIcon(linkIcon)
+			else:
+				sa = SortableTableWidgetItem(str(si),si)
+				item.setIcon(fileIcon)
 			sa.setTextAlignment(QtCore.Qt.AlignRight)
 			sa.setFlags(QtCore.Qt.ItemIsEnabled)
 			self.lb.setItem(i,2,sa)
@@ -307,6 +322,14 @@ class DirNotes(QMainWindow):
 	def restore_from_database(self):
 		print("restore from database")
 		fileName = str(self.lb.item(self.lb.currentRow(),0).text())
+		existing_comment = str(self.lb.item(self.lb.currentRow(),3).text())
+		print("restore....existing="+existing_comment+"=")
+		if len(existing_comment) > 0:
+			m = QMessageBox()
+			m.setText("This file already has a comment. Overwrite?")
+			m.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel);
+			if m.exec_() != QMessageBox.Ok:
+				return
 		fo_row = self.db.getData(fileName)
 		if fo_row and len(fo_row)>1:
 			comment = fo_row[3]
@@ -379,4 +402,3 @@ user.xdg.publisher
 			-choose & cancel buttons
 	
 	'''
-