Ejemplo n.º 1
0
 def saveDocument(self, doc):
     """ Saves the document, asking for a name if necessary.
     
     Returns True if saving succeeded.
     
     """
     if doc.url().isEmpty():
         return self.saveDocumentAs(doc)
     filename = dest = doc.url().toLocalFile()
     if not filename:
         dest = doc.url().toString()
     if not util.iswritable(filename):
         QMessageBox.warning(
             self, app.caption(_("Error")),
             _("Can't write to destination:\n\n{url}").format(url=dest))
         return False
     if QSettings().value("strip_trailing_whitespace", False, bool):
         import reformat
         reformat.remove_trailing_whitespace(QTextCursor(doc))
     b = backup.backup(filename)
     success = doc.save()
     if not success:
         QMessageBox.warning(
             self, app.caption(_("Error")),
             _("Can't write to destination:\n\n{url}").format(url=filename))
     elif b:
         backup.removeBackup(filename)
     return success
Ejemplo n.º 2
0
 def saveDocument(self, doc):
     """ Saves the document, asking for a name if necessary.
     
     Returns True if saving succeeded.
     
     """
     if doc.url().isEmpty():
         return self.saveDocumentAs(doc)
     filename = dest = doc.url().toLocalFile()
     if not filename:
         dest = doc.url().toString()
     if not util.iswritable(filename):
         QMessageBox.warning(
             self, app.caption(_("Error")), _("Can't write to destination:\n\n{url}").format(url=dest)
         )
         return False
     b = backup.backup(filename)
     success = doc.save()
     if not success:
         QMessageBox.warning(
             self, app.caption(_("Error")), _("Can't write to destination:\n\n{url}").format(url=filename)
         )
     elif b:
         backup.removeBackup(filename)
     return success
Ejemplo n.º 3
0
 def saveDocument(self, doc):
     """ Saves the document, asking for a name if necessary.
     
     Returns True if saving succeeded.
     
     """
     if doc.url().isEmpty():
         return self.saveDocumentAs(doc)
     filename = dest = doc.url().toLocalFile()
     if not filename:
         dest = doc.url().toString()
     if not util.iswritable(filename):
         QMessageBox.warning(self, app.caption(_("Error")),
             _("Can't write to destination:\n\n{url}").format(url=dest))
         return False
     if QSettings().value("strip_trailing_whitespace", False, bool):
         import reformat
         reformat.remove_trailing_whitespace(QTextCursor(doc))
     b = backup.backup(filename)
     success = doc.save()
     if not success:
         QMessageBox.warning(self, app.caption(_("Error")),
             _("Can't write to destination:\n\n{url}").format(url=filename))
     elif b:
         backup.removeBackup(filename)
     return success
Ejemplo n.º 4
0
    def saveDocument(self, doc, save_as=False):
        """ Saves the document, asking for a name if necessary.

        If save_as is True, a name is always asked.
        Returns True if saving succeeded.

        """
        if save_as or doc.url().isEmpty():
            filename = doc.url().toLocalFile()
            if filename:
                filetypes = app.filetypes(os.path.splitext(filename)[1])
            else:
                # find a suitable directory to save to
                for d in self.historyManager.documents()[1::]:
                    if d.url().toLocalFile():
                        directory = os.path.dirname(d.url().toLocalFile())
                        break
                else:
                    directory = app.basedir() # default directory to save to
                
                import documentinfo
                import ly.lex
                filename = os.path.join(directory, documentinfo.defaultfilename(doc))
                filetypes = app.filetypes(ly.lex.extensions[documentinfo.mode(doc)])
            caption = app.caption(_("dialog title", "Save File"))
            filename = QFileDialog.getSaveFileName(self, caption, filename, filetypes)[0]
            if not filename:
                return False # cancelled
            url = QUrl.fromLocalFile(filename)
        else:
            url = doc.url()

        if QSettings().value("strip_trailing_whitespace", False, bool):
            import reformat
            reformat.remove_trailing_whitespace(QTextCursor(doc))

        # we only support local files for now
        filename = url.toLocalFile()
        b = backup.backup(filename)
        try:
            doc.save(url)
        except IOError as e:
            msg = _("{message}\n\n{strerror} ({errno})").format(
                message = _("Could not write to: {url}").format(url=filename),
                strerror = e.strerror,
                errno = e.errno)
            QMessageBox.critical(self, app.caption(_("Error")), msg)
            return False
        else:
            if b:
                backup.removeBackup(filename)
            recentfiles.add(doc.url())
        return True
Ejemplo n.º 5
0
    def saveDocument(self, doc, save_as=False):
        """ Saves the document, asking for a name if necessary.
        
        If save_as is True, a name is always asked.
        Returns True if saving succeeded.
        
        """
        if save_as or doc.url().isEmpty():
            filename = doc.url().toLocalFile()
            if filename:
                filetypes = app.filetypes(os.path.splitext(filename)[1])
            else:
                directory = app.basedir() # default directory to save to
                import documentinfo
                import ly.lex
                filename = os.path.join(directory, documentinfo.defaultfilename(doc))
                filetypes = app.filetypes(ly.lex.extensions[documentinfo.mode(doc)])
            caption = app.caption(_("dialog title", "Save File"))
            filename = QFileDialog.getSaveFileName(self, caption, filename, filetypes)
            if not filename:
                return False # cancelled
            url = QUrl.fromLocalFile(filename)
        else:
            url = doc.url()
        
        if QSettings().value("strip_trailing_whitespace", False, bool):
            import reformat
            reformat.remove_trailing_whitespace(QTextCursor(doc))

        # we only support local files for now
        filename = url.toLocalFile()
        b = backup.backup(filename)
        try:
            doc.save(url)
        except IOError as e:
            msg = _("{message}\n\n{strerror} ({errno})").format(
                message = _("Could not write to: {url}").format(url=filename),
                strerror = e.strerror,
                errno = e.errno)
            QMessageBox.critical(self, app.caption(_("Error")), msg)
            return False
        else:
            if b:
                backup.removeBackup(filename)
            recentfiles.add(doc.url())
        return True