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.)
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();
67
bool rejected = false;
70
70
QMimeType mt = mdb.mimeTypeForFile(hubItem.url().toLocalFile());
72
bool rejected = false;
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();
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 += ".";
87
89
dir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + QDir::separator();
90
destination = QString("%1.%2").arg(dir + filenameWithoutSuffix, suffix);
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)) {
96
TRANSLATORS: This string is used for renaming a copied file,
97
when a file with the same name already exists in user's
100
e.g. "Manual_Aquaris_E4.5_ubuntu_EN.pdf" will become
101
"Manual_Aquaris_E4.5_ubuntu_EN (copy 2).pdf"
103
where "2" is given by the argument "%1"
105
QString reformattedSuffix = QString(_("copy %1"));
107
QRegExp rx(" \\(" + reformattedSuffix.arg(QString("\\d+")) + "\\)");
108
int reformattedSuffixPos = filenameWithoutSuffix.lastIndexOf(rx);
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);
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)),
97
122
} while(QFile::exists(destination));
100
QFile::copy(hubItem.url().toLocalFile(), destination);
125
QFile::copy(hubItem.url().toLocalFile(), destination);