void filedialogInit() { if (cDialogLastPath().isEmpty()) { #ifdef Q_OS_WIN // hack to restore previous dir without hurting performance QSettings settings(QSettings::UserScope, qstr("QtProject")); settings.beginGroup(qstr("Qt")); QByteArray sd = settings.value(qstr("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); }
bool GetDefault( QPointer<QWidget> parent, QStringList &files, QByteArray &remoteContent, const QString &caption, const QString &filter, FileDialog::internal::Type type, QString startFile = QString()) { if (cDialogLastPath().isEmpty()) { Platform::FileDialog::InitLastPath(); } remoteContent = QByteArray(); if (startFile.isEmpty() || startFile.at(0) != '/') { startFile = cDialogLastPath() + '/' + startFile; } QString file; if (type == Type::ReadFiles) { files = QFileDialog::getOpenFileNames(Messenger::Instance().getFileDialogParent(), caption, startFile, filter); QString path = files.isEmpty() ? QString() : QFileInfo(files.back()).absoluteDir().absolutePath(); if (!path.isEmpty() && path != cDialogLastPath()) { cSetDialogLastPath(path); Local::writeUserSettings(); } return !files.isEmpty(); } else if (type == Type::ReadFolder) { file = QFileDialog::getExistingDirectory(Messenger::Instance().getFileDialogParent(), caption, startFile); } else if (type == Type::WriteFile) { file = QFileDialog::getSaveFileName(Messenger::Instance().getFileDialogParent(), caption, startFile, filter); } else { file = QFileDialog::getOpenFileName(Messenger::Instance().getFileDialogParent(), caption, startFile, filter); } if (file.isEmpty()) { files = QStringList(); return false; } if (type != Type::ReadFolder) { // Save last used directory for all queries except directory choosing. auto path = QFileInfo(file).absoluteDir().absolutePath(); if (!path.isEmpty() && path != cDialogLastPath()) { cSetDialogLastPath(path); Local::writeUserSettings(); } } files = QStringList(file); return true; }
QString filedialogDefaultName( const QString &prefix, const QString &extension, const QString &path, bool skipExistance, TimeId fileTime) { auto directoryPath = path; if (directoryPath.isEmpty()) { if (cDialogLastPath().isEmpty()) { Platform::FileDialog::InitLastPath(); } directoryPath = cDialogLastPath(); } QString base; if (fileTime) { const auto date = ParseDateTime(fileTime); base = prefix + date.toString("_yyyy-MM-dd_HH-mm-ss"); } else { struct tm tm; time_t t = time(NULL); mylocaltime(&tm, &t); QChar zero('0'); 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); } QString name; if (skipExistance) { name = base + extension; } else { QDir directory(directoryPath); const auto dir = directory.absolutePath(); const auto nameBase = (dir.endsWith('/') ? dir : (dir + '/')) + base; name = nameBase + extension; for (int i = 0; QFileInfo(name).exists(); ++i) { name = nameBase + qsl(" (%1)").arg(i + 2) + extension; } } return name; }
void DownloadPathBox::onEditPath() { filedialogInit(); QString path, lastPath = cDialogLastPath(); if (!cDownloadPath().isEmpty() && cDownloadPath() != qstr("tmp")) { cSetDialogLastPath(cDownloadPath().left(cDownloadPath().size() - (cDownloadPath().endsWith('/') ? 1 : 0))); } if (filedialogGetDir(path, lang(lng_download_path_choose))) { if (!path.isEmpty()) { _path = path + '/'; _pathBookmark = psDownloadPathBookmark(_path); setPathText(QDir::toNativeSeparators(_path)); } } cSetDialogLastPath(lastPath); }
void DownloadPathBox::onEditPath() { _dirInput.clearFocus(); filedialogInit(); QString path, lastPath = cDialogLastPath(); if (!cDownloadPath().isEmpty()) cSetDialogLastPath(cDownloadPath()); if (filedialogGetDir(path, lang(lng_download_path_choose))) { if (!path.isEmpty()) { _path = path + '/'; _dirInput.setText(QDir::toNativeSeparators(_path)); _dirInput.setCursorPosition(0); } } cSetDialogLastPath(lastPath); }
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; }