~verzegnassi-stefano/ubuntu-docviewer-app/reboot-document-last-position

« back to all changes in this revision

Viewing changes to src/app/content-communicator.cpp

- Rename a file as 'filename (copy x).ext' (instead of 'filename.x.ext') if a file with the same name already exists.
- Fixed bad renaming of a file that already has "(copy x)" suffix in its name. Fixes: https://bugs.launchpad.net/bugs/1432403, https://bugs.launchpad.net/bugs/1432408.

Approved by Ubuntu Phone Apps Jenkins Bot, Stefano Verzegnassi, Alan Pope .

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
{
58
58
    // FIXME: If a file is imported from $HOME/Documents, a new copy of the file is created.
59
59
    //   Could be use md5? http://doc.qt.io/qt-5/qml-qtqml-qt.html#md5-method
60
 
    // FIXME: If there already a file called "filename.1.ext", the imported file won't be renamed as "filename.2.ext", but "filename.1.1.ext".
61
 
    //  (This issue is in gallery-app too.)
62
 
 
63
60
    QVariantList importedDocuments;
64
61
    QVector<Item> transferedItems = transfer->collect();
65
62
    foreach (const Item &hubItem, transferedItems) {
66
63
        QFileInfo fi(hubItem.url().toLocalFile());
67
 
        QString filename = fi.fileName();
 
64
 
68
65
        QString dir;
 
66
        QString destination;
 
67
        bool rejected = false;
 
68
 
69
69
        QMimeDatabase mdb;
70
70
        QMimeType mt = mdb.mimeTypeForFile(hubItem.url().toLocalFile());
71
 
        QString destination;
72
 
        bool rejected = false;
73
71
 
74
72
        // Check if the item is supported by Ubuntu Document Viewer
75
73
        if (isSupportedMimetype(mt.name())) {
76
 
            QString suffix = fi.completeSuffix();
77
 
            // FIXME: Should we use fi.baseName()?
78
 
            QString filenameWithoutSuffix = filename.left(filename.size() - suffix.size());
 
74
            /* We don't support formats that use a double extension
 
75
               (e.g. tar.gz), so we can safely use completeBaseName() and
 
76
               suffix() functions, in order to properly detect the name of
 
77
               the document even when there's a dot in the middle of the name.*/
 
78
            QString suffix = fi.suffix();
 
79
            QString filenameWithoutSuffix = fi.completeBaseName();
 
80
 
79
81
            if(suffix.isEmpty()) {
80
82
                // If the filename doesn't have an extension add one from the
81
83
                // detected mimetype
82
84
                if(!mt.preferredSuffix().isEmpty()) {
83
85
                    suffix = mt.preferredSuffix();
84
 
                    filenameWithoutSuffix += ".";
85
86
                }
86
87
            }
 
88
 
87
89
            dir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + QDir::separator();
 
90
            destination = QString("%1.%2").arg(dir + filenameWithoutSuffix, suffix);
88
91
 
89
 
            destination = QString("%1%2").arg(dir + filenameWithoutSuffix, suffix);
90
 
            // If we already have a file of this name reformat to "filename.x.png"
 
92
            // If we already have a file of this name reformat to "filename (copy x).png"
91
93
            // (where x is a number, incremented until we find an available filename)
92
94
            if(QFile::exists(destination)) {
 
95
                /*
 
96
                 TRANSLATORS: This string is used for renaming a copied file,
 
97
                 when a file with the same name already exists in user's
 
98
                 Documents folder.
 
99
 
 
100
                 e.g. "Manual_Aquaris_E4.5_ubuntu_EN.pdf" will become
 
101
                      "Manual_Aquaris_E4.5_ubuntu_EN (copy 2).pdf"
 
102
 
 
103
                      where "2" is given by the argument "%1"
 
104
                */
 
105
                QString reformattedSuffix = QString(_("copy %1"));
 
106
 
 
107
                QRegExp rx(" \\(" + reformattedSuffix.arg(QString("\\d+")) + "\\)");
 
108
                int reformattedSuffixPos = filenameWithoutSuffix.lastIndexOf(rx);
 
109
 
 
110
                // Check if the file has already a "copy" suffix
 
111
                if(reformattedSuffixPos != -1) {
 
112
                    // Remove the "copy" suffix. We will re-put it later.
 
113
                    filenameWithoutSuffix.truncate(reformattedSuffixPos);
 
114
                }
 
115
 
93
116
                int append = 1;
94
117
                do {
95
 
                    destination = QString("%1%2.%3").arg(dir + filenameWithoutSuffix, QString::number(append), suffix);
 
118
                    destination = QString("%1 (%2).%3").arg(dir + filenameWithoutSuffix,
 
119
                                                            reformattedSuffix.arg(QString::number(append)),
 
120
                                                            suffix);
96
121
                    append++;
97
122
                } while(QFile::exists(destination));
98
123
            }
99
124
 
100
 
            QFile::copy(hubItem.url().toLocalFile(), destination);           
 
125
            QFile::copy(hubItem.url().toLocalFile(), destination);
101
126
        } else {
102
127
            rejected = true;
103
128
        }
106
131
        // emitted with the 'documentImported' signal.
107
132
        QVariantMap entry;
108
133
        if (rejected) {
109
 
            entry["fileName"] = filename;
 
134
            entry["fileName"] = fi.fileName();
110
135
        } else {
111
136
            entry["fileName"] = destination;
112
137
        }