Changeset View
Changeset View
Standalone View
Standalone View
src/SaveHistoryTask.cpp
Show All 20 Lines | |||||
21 | #include "SaveHistoryTask.h" | 21 | #include "SaveHistoryTask.h" | ||
22 | 22 | | |||
23 | #include <QFileDialog> | 23 | #include <QFileDialog> | ||
24 | #include <QApplication> | 24 | #include <QApplication> | ||
25 | #include <QTextStream> | 25 | #include <QTextStream> | ||
26 | 26 | | |||
27 | #include <KMessageBox> | 27 | #include <KMessageBox> | ||
28 | #include <KLocalizedString> | 28 | #include <KLocalizedString> | ||
29 | #include <KSharedConfig> | ||||
30 | #include <KConfig> | ||||
31 | #include <KConfigGroup> | ||||
29 | 32 | | |||
30 | #include "SessionManager.h" | 33 | #include "SessionManager.h" | ||
31 | #include "Emulation.h" | 34 | #include "Emulation.h" | ||
32 | 35 | | |||
33 | namespace Konsole { | 36 | namespace Konsole { | ||
34 | 37 | | |||
38 | QString SaveHistoryTask::_saveDialogRecentURL; | ||||
39 | | ||||
35 | SaveHistoryTask::SaveHistoryTask(QObject* parent) | 40 | SaveHistoryTask::SaveHistoryTask(QObject* parent) | ||
36 | : SessionTask(parent) | 41 | : SessionTask(parent) | ||
37 | { | 42 | { | ||
38 | } | 43 | } | ||
39 | 44 | | |||
40 | SaveHistoryTask::~SaveHistoryTask() = default; | 45 | SaveHistoryTask::~SaveHistoryTask() = default; | ||
41 | 46 | | |||
42 | void SaveHistoryTask::execute() | 47 | void SaveHistoryTask::execute() | ||
43 | { | 48 | { | ||
44 | // TODO - think about the UI when saving multiple history sessions, if there are more than two or | 49 | // TODO - think about the UI when saving multiple history sessions, if there are more than two or | ||
45 | // three then providing a URL for each one will be tedious | 50 | // three then providing a URL for each one will be tedious | ||
46 | 51 | | |||
47 | // TODO - show a warning ( preferably passive ) if saving the history output fails | 52 | // TODO - show a warning ( preferably passive ) if saving the history output fails | ||
48 | QFileDialog* dialog = new QFileDialog(QApplication::activeWindow(), | 53 | QFileDialog* dialog = new QFileDialog(QApplication::activeWindow()); | ||
49 | QString(), | | |||
50 | QDir::homePath()); | | |||
51 | dialog->setAcceptMode(QFileDialog::AcceptSave); | 54 | dialog->setAcceptMode(QFileDialog::AcceptSave); | ||
52 | 55 | | |||
53 | QStringList mimeTypes { | 56 | QStringList mimeTypes { | ||
54 | QStringLiteral("text/plain"), | 57 | QStringLiteral("text/plain"), | ||
55 | QStringLiteral("text/html") | 58 | QStringLiteral("text/html") | ||
56 | }; | 59 | }; | ||
57 | dialog->setMimeTypeFilters(mimeTypes); | 60 | dialog->setMimeTypeFilters(mimeTypes); | ||
58 | 61 | | |||
62 | KSharedConfigPtr konsoleConfig = KSharedConfig::openConfig(); | ||||
63 | auto group = konsoleConfig->group("SaveHistory Settings"); | ||||
64 | | ||||
65 | if (_saveDialogRecentURL.isEmpty()) { | ||||
66 | const auto list = group.readPathEntry("Recent URLs", QStringList()); | ||||
67 | if (list.isEmpty()) { | ||||
68 | dialog->setDirectory(QDir::homePath()); | ||||
69 | } else { | ||||
70 | dialog->setDirectoryUrl(QUrl(list.at(0))); | ||||
71 | } | ||||
72 | } else { | ||||
73 | dialog->setDirectoryUrl(QUrl(_saveDialogRecentURL)); | ||||
74 | } | ||||
75 | | ||||
59 | // iterate over each session in the task and display a dialog to allow the user to choose where | 76 | // iterate over each session in the task and display a dialog to allow the user to choose where | ||
60 | // to save that session's history. | 77 | // to save that session's history. | ||
61 | // then start a KIO job to transfer the data from the history to the chosen URL | 78 | // then start a KIO job to transfer the data from the history to the chosen URL | ||
62 | foreach(const auto& session, sessions()) { | 79 | foreach(const auto& session, sessions()) { | ||
63 | dialog->setWindowTitle(i18n("Save Output From %1", session->title(Session::NameRole))); | 80 | dialog->setWindowTitle(i18n("Save Output From %1", session->title(Session::NameRole))); | ||
64 | 81 | | |||
65 | int result = dialog->exec(); | 82 | int result = dialog->exec(); | ||
66 | 83 | | |||
67 | if (result != QDialog::Accepted) { | 84 | if (result != QDialog::Accepted) { | ||
68 | continue; | 85 | continue; | ||
69 | } | 86 | } | ||
70 | 87 | | |||
71 | QUrl url = (dialog->selectedUrls()).at(0); | 88 | QUrl url = (dialog->selectedUrls()).at(0); | ||
72 | 89 | | |||
73 | if (!url.isValid()) { | 90 | if (!url.isValid()) { | ||
74 | // UI: Can we make this friendlier? | 91 | // UI: Can we make this friendlier? | ||
75 | KMessageBox::sorry(nullptr , i18n("%1 is an invalid URL, the output could not be saved.", url.url())); | 92 | KMessageBox::sorry(nullptr , i18n("%1 is an invalid URL, the output could not be saved.", url.url())); | ||
76 | continue; | 93 | continue; | ||
77 | } | 94 | } | ||
78 | 95 | | |||
96 | // Save selected URL for next time | ||||
mglb: `url` is a file, not current directory. `dialog->directoryUrl()` should work | |||||
@mglb
For 1,2 file is better For me, I use case 2 most time during working. guotao: @mglb
I think there are 4 cases
1. User want overwrite the last output file
2. User don't want… | |||||
Good idea. It asks for overwrite confirmation, the file name is already selected (so typing another name takes as much work as when there were no name). No downsides. mglb: Good idea. It asks for overwrite confirmation, the file name is already selected (so typing… | |||||
97 | _saveDialogRecentURL = url.adjusted(QUrl::RemoveFilename|QUrl::StripTrailingSlash).toString(); | ||||
98 | group.writePathEntry("Recent URLs", _saveDialogRecentURL); | ||||
99 | | ||||
79 | KIO::TransferJob* job = KIO::put(url, | 100 | KIO::TransferJob* job = KIO::put(url, | ||
80 | -1, // no special permissions | 101 | -1, // no special permissions | ||
81 | // overwrite existing files | 102 | // overwrite existing files | ||
82 | // do not resume an existing transfer | 103 | // do not resume an existing transfer | ||
83 | // show progress information only for remote | 104 | // show progress information only for remote | ||
84 | // URLs | 105 | // URLs | ||
85 | KIO::Overwrite | (url.isLocalFile() ? KIO::HideProgressInfo : KIO::DefaultFlags) | 106 | KIO::Overwrite | (url.isLocalFile() ? KIO::HideProgressInfo : KIO::DefaultFlags) | ||
86 | // a better solution would be to show progress | 107 | // a better solution would be to show progress | ||
▲ Show 20 Lines • Show All 84 Lines • Show Last 20 Lines |
url is a file, not current directory. dialog->directoryUrl() should work