2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2014-12-01 10:47:38 +00:00
|
|
|
Copyright (c) 2014 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "gui/filedialog.h"
|
|
|
|
|
|
|
|
#include "app.h"
|
|
|
|
#include "application.h"
|
|
|
|
|
|
|
|
void filedialogInit() {
|
|
|
|
if (cDialogLastPath().isEmpty()) {
|
2014-09-22 03:52:37 +00:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
// hack to restore previous dir without hurting performance
|
2014-05-30 08:53:19 +00:00
|
|
|
QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
|
|
|
|
settings.beginGroup(QLatin1String("Qt"));
|
|
|
|
QByteArray sd = settings.value(QLatin1String("filedialog")).toByteArray();
|
|
|
|
QDataStream stream(&sd, QIODevice::ReadOnly);
|
|
|
|
if (!stream.atEnd()) {
|
|
|
|
int version = 3, _QFileDialogMagic = 190;
|
|
|
|
QByteArray splitterState;
|
|
|
|
QByteArray headerData;
|
|
|
|
QList<QUrl> bookmarks;
|
|
|
|
QStringList history;
|
|
|
|
QString currentDirectory;
|
|
|
|
qint32 marker;
|
|
|
|
qint32 v;
|
|
|
|
qint32 viewMode;
|
|
|
|
stream >> marker;
|
|
|
|
stream >> v;
|
|
|
|
if (marker == _QFileDialogMagic && v == version) {
|
|
|
|
stream >> splitterState
|
|
|
|
>> bookmarks
|
|
|
|
>> history
|
|
|
|
>> currentDirectory
|
|
|
|
>> headerData
|
|
|
|
>> viewMode;
|
|
|
|
cSetDialogLastPath(currentDirectory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cDialogHelperPath().isEmpty()) {
|
|
|
|
QDir temppath(cWorkingDir() + "tdata/tdummy/");
|
|
|
|
if (!temppath.exists()) {
|
|
|
|
temppath.mkpath(temppath.absolutePath());
|
|
|
|
}
|
|
|
|
if (temppath.exists()) {
|
|
|
|
cSetDialogHelperPath(temppath.absolutePath());
|
|
|
|
}
|
|
|
|
}
|
2014-09-22 03:52:37 +00:00
|
|
|
#else
|
|
|
|
cSetDialogLastPath(QStandardPaths::writableLocation(QStandardPaths::DownloadLocation));
|
|
|
|
#endif
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-24 16:00:35 +00:00
|
|
|
// multipleFiles: 1 - multi open, 0 - single open, -1 - single save, -2 - select dir
|
2014-09-22 03:52:37 +00:00
|
|
|
bool _filedialogGetFiles(QStringList &files, QByteArray &remoteContent, const QString &caption, const QString &filter, int multipleFiles, QString startFile = QString()) {
|
2014-07-24 16:00:35 +00:00
|
|
|
#if defined Q_OS_LINUX || defined Q_OS_MAC // use native
|
|
|
|
remoteContent = QByteArray();
|
|
|
|
QString file;
|
|
|
|
if (multipleFiles >= 0) {
|
2014-08-11 09:03:45 +00:00
|
|
|
files = QFileDialog::getOpenFileNames(App::wnd() ? App::wnd()->filedialogParent() : 0, caption, startFile, filter);
|
2014-09-22 03:52:37 +00:00
|
|
|
QString path = files.isEmpty() ? QString() : QFileInfo(files.back()).absoluteDir().absolutePath();
|
|
|
|
if (!path.isEmpty()) cSetDialogLastPath(path);
|
2014-07-24 16:00:35 +00:00
|
|
|
return !files.isEmpty();
|
|
|
|
} else if (multipleFiles < -1) {
|
2014-08-11 09:03:45 +00:00
|
|
|
file = QFileDialog::getExistingDirectory(App::wnd() ? App::wnd()->filedialogParent() : 0, caption);
|
2014-07-24 16:00:35 +00:00
|
|
|
} else if (multipleFiles < 0) {
|
2014-08-11 09:03:45 +00:00
|
|
|
file = QFileDialog::getSaveFileName(App::wnd() ? App::wnd()->filedialogParent() : 0, caption, startFile, filter);
|
2014-07-24 16:00:35 +00:00
|
|
|
} else {
|
2014-08-11 09:03:45 +00:00
|
|
|
file = QFileDialog::getOpenFileName(App::wnd() ? App::wnd()->filedialogParent() : 0, caption, startFile, filter);
|
2014-07-24 16:00:35 +00:00
|
|
|
}
|
|
|
|
if (file.isEmpty()) {
|
|
|
|
files = QStringList();
|
|
|
|
return false;
|
|
|
|
} else {
|
2014-09-22 03:52:37 +00:00
|
|
|
QString path = QFileInfo(file).absoluteDir().absolutePath();
|
|
|
|
if (!path.isEmpty()) cSetDialogLastPath(path);
|
2014-07-24 16:00:35 +00:00
|
|
|
files = QStringList(file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
filedialogInit();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
// hack for fast non-native dialog create
|
2014-08-11 09:03:45 +00:00
|
|
|
QFileDialog dialog(App::wnd() ? App::wnd()->filedialogParent() : 0, caption, cDialogHelperPathFinal(), filter);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2014-07-24 16:00:35 +00:00
|
|
|
dialog.setModal(true);
|
2014-05-30 08:53:19 +00:00
|
|
|
if (multipleFiles >= 0) { // open file or files
|
|
|
|
dialog.setFileMode(multipleFiles ? QFileDialog::ExistingFiles : QFileDialog::ExistingFile);
|
|
|
|
dialog.setAcceptMode(QFileDialog::AcceptOpen);
|
|
|
|
} else if (multipleFiles < -1) { // save dir
|
|
|
|
dialog.setAcceptMode(QFileDialog::AcceptOpen);
|
|
|
|
dialog.setFileMode(QFileDialog::Directory);
|
|
|
|
dialog.setOption(QFileDialog::ShowDirsOnly);
|
|
|
|
} else { // save file
|
|
|
|
dialog.setFileMode(QFileDialog::AnyFile);
|
|
|
|
dialog.setAcceptMode(QFileDialog::AcceptSave);
|
|
|
|
}
|
|
|
|
dialog.show();
|
|
|
|
|
|
|
|
if (!cDialogLastPath().isEmpty()) dialog.setDirectory(cDialogLastPath());
|
|
|
|
if (multipleFiles == -1) {
|
|
|
|
QString toSelect(startFile);
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
int32 lastSlash = toSelect.lastIndexOf('/');
|
|
|
|
if (lastSlash >= 0) {
|
|
|
|
toSelect = toSelect.mid(lastSlash + 1);
|
|
|
|
}
|
|
|
|
int32 lastBackSlash = toSelect.lastIndexOf('\\');
|
|
|
|
if (lastBackSlash >= 0) {
|
|
|
|
toSelect = toSelect.mid(lastBackSlash + 1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
dialog.selectFile(toSelect);
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = dialog.exec();
|
|
|
|
|
|
|
|
cSetDialogLastPath(dialog.directory().absolutePath());
|
|
|
|
|
|
|
|
if (res == QDialog::Accepted) {
|
|
|
|
if (multipleFiles > 0) {
|
|
|
|
files = dialog.selectedFiles();
|
|
|
|
} else {
|
|
|
|
files = dialog.selectedFiles().mid(0, 1);
|
|
|
|
}
|
|
|
|
if (multipleFiles >= 0) {
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
remoteContent = dialog.selectedRemoteContent();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
files = QStringList();
|
|
|
|
remoteContent = QByteArray();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool filedialogGetOpenFiles(QStringList &files, QByteArray &remoteContent, const QString &caption, const QString &filter) {
|
|
|
|
return _filedialogGetFiles(files, remoteContent, caption, filter, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool filedialogGetOpenFile(QString &file, QByteArray &remoteContent, const QString &caption, const QString &filter) {
|
|
|
|
QStringList files;
|
|
|
|
bool result = _filedialogGetFiles(files, remoteContent, caption, filter, 0);
|
|
|
|
file = files.isEmpty() ? QString() : files.at(0);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool filedialogGetSaveFile(QString &file, const QString &caption, const QString &filter, const QString &startName) {
|
|
|
|
QStringList files;
|
|
|
|
QByteArray remoteContent;
|
|
|
|
bool result = _filedialogGetFiles(files, remoteContent, caption, filter, -1, startName);
|
|
|
|
file = files.isEmpty() ? QString() : files.at(0);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool filedialogGetDir(QString &dir, const QString &caption) {
|
|
|
|
QStringList files;
|
|
|
|
QByteArray remoteContent;
|
|
|
|
bool result = _filedialogGetFiles(files, remoteContent, caption, QString(), -2);
|
|
|
|
dir = files.isEmpty() ? QString() : files.at(0);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-08-11 09:03:45 +00:00
|
|
|
QString filedialogDefaultName(const QString &prefix, const QString &extension, const QString &path, bool skipExistance) {
|
2014-05-30 08:53:19 +00:00
|
|
|
filedialogInit();
|
|
|
|
|
|
|
|
time_t t = time(NULL);
|
|
|
|
struct tm tm;
|
|
|
|
mylocaltime(&tm, &t);
|
|
|
|
|
|
|
|
QChar zero('0');
|
|
|
|
|
2014-08-11 09:03:45 +00:00
|
|
|
QString name;
|
2014-10-17 12:57:14 +00:00
|
|
|
QString base = prefix + qsl("_%1-%2-%3_%4-%5-%6").arg(tm.tm_year + 1900).arg(tm.tm_mon + 1, 2, 10, zero).arg(tm.tm_mday, 2, 10, zero).arg(tm.tm_hour, 2, 10, zero).arg(tm.tm_min, 2, 10, zero).arg(tm.tm_sec, 2, 10, zero);
|
2014-08-11 09:03:45 +00:00
|
|
|
if (skipExistance) {
|
|
|
|
name = base + extension;
|
|
|
|
} else {
|
|
|
|
QDir dir(path.isEmpty() ? cDialogLastPath() : path);
|
|
|
|
QString nameBase = dir.absolutePath() + '/' + base;
|
|
|
|
name = nameBase + extension;
|
|
|
|
for (int i = 0; QFileInfo(name).exists(); ++i) {
|
2014-10-17 12:57:14 +00:00
|
|
|
name = nameBase + qsl(" (%1)").arg(i + 2) + extension;
|
2014-08-11 09:03:45 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
2014-10-17 12:57:14 +00:00
|
|
|
|
|
|
|
QString filedialogNextFilename(const QString &name, const QString &cur, const QString &path) {
|
|
|
|
QDir dir(path.isEmpty() ? cDialogLastPath() : path);
|
|
|
|
int32 extIndex = name.lastIndexOf('.');
|
|
|
|
QString prefix = name, extension;
|
|
|
|
if (extIndex >= 0) {
|
|
|
|
extension = name.mid(extIndex);
|
|
|
|
prefix = name.mid(0, extIndex);
|
|
|
|
}
|
|
|
|
QString nameBase = dir.absolutePath() + '/' + prefix, result = nameBase + extension;
|
|
|
|
for (int i = 0; result.toLower() != cur.toLower() && QFileInfo(result).exists(); ++i) {
|
|
|
|
result = nameBase + qsl(" (%1)").arg(i + 2) + extension;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|