def __addCreateActions(self, menu): newMenu = menu.addMenu(QtGui.QIcon("icons/filenew.png"), "New document") action = newMenu.addAction(QtGui.QIcon("icons/uti/folder.png"), "Folder") action.triggered.connect(self.__doCreateFolder) newMenu.addSeparator() items = {} sysStore = Connector().enum().sysStore().sid sysDict = struct.Folder(connector.DocLink(sysStore, sysStore)) templatesDoc = sysDict.get("templates") if templatesDoc: templatesDict = struct.Folder(templatesDoc.update(sysStore)) items = templatesDict.items() items.sort(key=lambda item: item[0]) if items: for (name, link) in items: rev = link.rev() icon = QtGui.QIcon(Registry().getIcon( Connector().stat(rev).type())) action = newMenu.addAction(icon, name) action.triggered.connect( lambda x, r=rev, n=name: self.__doCreateFromTemplate( sysStore, r, n)) else: action = newMenu.addAction("No templates found") action.setEnabled(False)
def __addReplicateActions(self, menu, link): c = Connector() try: allVolumes = set(c.lookupRev(self.rev())) if isinstance(link, connector.DocLink): lookup = c.lookupDoc(link.doc()) curVolumes = set(lookup.stores()) try: for rev in lookup.revs(): curVolumes = curVolumes & set( c.lookupRev(rev, curVolumes)) except IOError: curVolumes = set() else: curVolumes = set(c.lookupRev(link.rev())) except IOError: return if not curVolumes: return srcVol = list(curVolumes)[0] repVolumes = allVolumes - curVolumes for store in repVolumes: name = struct.readTitle(connector.DocLink(store, store), "Unknown store") action = menu.addAction("Replicate item to '%s'" % name) action.triggered.connect( lambda x, l=link, s=store: self.__doReplicate(srcVol, l, s))
def __doCreateFolder(self): store = self.store() with Connector().create(store, "org.peerdrive.folder", "") as w: w.setData( '', { "org.peerdrive.annotation": { "title": "Folder" }, "org.peerdrive.folder": [] }) w.setFlags([Stat.FLAG_STICKY]) w.commit("Created") self.model().insertLink(connector.DocLink(store, w.getDoc())) self.save("Added new folder")
def __doCreateFromTemplate(self, srcStore, srcRev, name): info = Connector().stat(srcRev, [srcStore]) dstStore = self.store() with Connector().create(dstStore, info.type(), info.creator()) as w: with Connector().peek(srcStore, srcRev) as r: w.set_data('', r.get_data('')) for att in info.attachments(): w.write(att, r.readAll(att)) w.setFlags(r.stat().flags()) w.commit("Created from template") destDoc = w.getDoc() # add link self.model().insertLink(connector.DocLink(dstStore, destDoc)) # save immediately self.save("Added '" + name + "' from templates")
def __dropLinks(self, links): if not links: return False dropMenu = QtGui.QMenu() repAct = dropMenu.addAction("Replicate here") copyAct = dropMenu.addAction("Copy here") dropMenu.addSeparator() dropMenu.addAction("Abort") copyAct.setEnabled( any([isinstance(l, connector.DocLink) for l in links])) action = dropMenu.exec_(QtGui.QCursor.pos()) if action is repAct: for link in links: if self.validateDragEnter(link): handle = ReplicateHelper(self.__parent, self.__store, link) try: self.insertLink(link) self.__parent.save() finally: handle.close() elif action is copyAct: for link in links: if isinstance(link, connector.RevLink): if self.validateDragEnter(link): handle = ReplicateHelper(self.__parent, self.__store, link) try: self.insertLink(link) self.__parent.save() finally: handle.close() else: with struct.copyDoc(link, self.__store) as handle: self.insertLink( connector.DocLink(self.__store, handle.getDoc())) self.__parent.save() else: return False return True
def __dropContents(self, mime): # unfortunately Qt will only return the first object and nothing # in case of Outlook messages content = str(mime.data('FileContents')) if len(content) == 0: return False name = getFileNamesFromMime(mime)[0] ext = os.path.splitext(name)[1].lower() uti = Registry().getUtiFromExtension(ext) data = {"org.peerdrive.annotation": {"title": name}}, spec = [('_', content)], handle = importer.importObject(self.__store, uti, data, spec, []) if handle: try: self.insertLink( connector.DocLink(self.__store, handle.getDoc())) self.__parent.save() finally: handle.close() return True
def __dropFile(self, data, onto): urlList = data.urls() if onto.isValid(): if len(urlList) != 1: choice = QtGui.QMessageBox.question( self.__parent, "Overwrite", "Cannot overwrite: more than one dragged file. Import instead?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No) if choice != QtGui.QMessageBox.Yes: return False elif not os.path.isfile(str(urlList[0].toLocalFile().toUtf8())): choice = QtGui.QMessageBox.question( self.__parent, "Overwrite", "Cannot overwrite: dragged item is not a file. Import instead?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No) if choice != QtGui.QMessageBox.Yes: return False else: choice = QtGui.QMessageBox.question( self.__parent, "Overwrite", "Do you want to overwrite the selected item? If not, the file will be imported as new item.", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No | QtGui.QMessageBox.Cancel, QtGui.QMessageBox.No) if choice == QtGui.QMessageBox.Cancel: return False elif choice == QtGui.QMessageBox.Yes: link = self.getItemLink(onto) path = str(urlList[0].toLocalFile().toUtf8()) try: return importer.overwriteFile(link, path) except IOError: pass except OSError: pass return False # count the number of files progress = QtGui.QProgressDialog("Counting files...", "Abort", 0, len(urlList), self.__parent) progress.setWindowModality(QtCore.Qt.WindowModal) progress.setMinimumDuration(500) numFiles = 0 i = 0 try: for url in urlList: progress.setValue(i) if progress.wasCanceled(): return False numFiles += countFilesRecursive(str( url.toLocalFile().toUtf8())) i += 1 finally: progress.setValue(len(urlList)) # import and add to folder progress = QtGui.QProgressDialog("Importing files...", "Abort", 0, numFiles, self.__parent) progress.setWindowModality(QtCore.Qt.WindowModal) progress.setMinimumDuration(500) try: helper = makeProgressHelper(progress) for url in urlList: path = str(url.toLocalFile().toUtf8()) handle = importer.importFile(self.__store, path, progress=helper) if handle: try: self.insertLink( connector.DocLink(self.__store, handle.getDoc())) self.__parent.save() finally: handle.close() except AbortException: pass finally: progress.setValue(numFiles) return True
def __showSelfProperties(self): if self.doc(): link = connector.DocLink(self.store(), self.doc(), False) else: link = connector.RevLink(self.store(), self.rev()) utils.showProperties(link)