importkradiorippersettings.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "importkradiorippersettings.h"
00022
00023 #include <QFile>
00024 #include <QFileInfo>
00025 #include <QTextStream>
00026 #include <KConfig>
00027 #include <KConfigGroup>
00028 #include <KLocalizedString>
00029 #include <KMessageBox>
00030 #include <KStandardDirs>
00031 #include <KTemporaryFile>
00032 #include <KUrl>
00033 #include "includekio.h"
00034 #include "settings_general.h"
00035
00036 void importKradioripperSettings::importSettingsIfDesired()
00037 {
00038 KConfigGroup config_group(settings_general::self()->config(), "kradioripperimport");
00039 if (config_group.hasKey("importRequested") &&
00040 config_group.readEntry<bool>("importRequested", false)) {
00041 importSettings();
00042 KMessageBox::information(0, i18nc("@info", "KRadioRipper's settings have been imported."));
00043 };
00044 }
00045
00046 QString importKradioripperSettings::allocateStreamFile()
00047 {
00048
00049 QFileInfo info;
00050 QString returnValue;
00051
00052
00053 KTemporaryFile newConfigFile;
00054
00055
00056 newConfigFile.setPrefix(helper_locateConfigfile("stream_"));
00057 newConfigFile.setSuffix("_rc");
00058 newConfigFile.open();
00059 newConfigFile.setAutoRemove(false);
00060 info.setFile(newConfigFile.fileName());
00061 returnValue = info.fileName();
00062 newConfigFile.close();
00063 return returnValue;
00064 }
00065
00066 QString importKradioripperSettings::helper_locateConfigfile(const QString & configfileName)
00067 {
00068 return KStandardDirs::locateLocal("appdata", configfileName);
00069 }
00070
00071 inline QString importKradioripperSettings::helper_stationlistXmlFilepath()
00072 {
00073 return KStandardDirs::locateLocal("appdata", "stationlist.xml");
00074 }
00075
00076 void importKradioripperSettings::importSettings()
00077 {
00078
00079 KIO::NetAccess::synchronousRun(
00080 KIO::file_copy(
00081 KUrl(KStandardDirs::locate("config", "kradioripperrc")),
00082 KUrl(KStandardDirs::locate("config", settings_general::self()->config()->name())),
00083 -1,
00084 KIO::HideProgressInfo|KIO::Overwrite),
00085 0);
00086
00087
00088 settings_general::self()->readConfig();
00089
00090
00091 KConfigGroup group_streamlist(settings_general::self()->config(), "internal");
00092 QStringList old_list_FileFullNames =
00093 group_streamlist.readPathEntry("streamConfigFiles", QStringList());
00094 QString xmlSnippet = " <folder name=\"KRadioRipper\">\n";
00095 foreach (const QString & old_fileFullName, old_list_FileFullNames) {
00096 QString new_fileSimpleName = allocateStreamFile();
00097 KIO::NetAccess::synchronousRun(
00098 KIO::file_copy(
00099 KUrl(old_fileFullName),
00100 KUrl(KStandardDirs::locate("appdata", new_fileSimpleName)),
00101 -1,
00102 KIO::HideProgressInfo|KIO::Overwrite),
00103 0);
00104 xmlSnippet += (" <stream>" + new_fileSimpleName + "</stream>\n");
00105 };
00106 xmlSnippet += " </folder>\n</stationlist>";
00107 group_streamlist.deleteEntry("streamConfigFiles");
00108 group_streamlist.sync();
00109 QFile xmlFile(helper_stationlistXmlFilepath());
00110 xmlFile.open(QIODevice::ReadWrite);
00111
00112
00113 QString newXmlFileContent = QTextStream(&xmlFile).readAll().replace("</stationlist>", xmlSnippet);
00114 QFile xmlNewFile(helper_stationlistXmlFilepath() + ".new");
00115 xmlNewFile.open(QIODevice::ReadWrite);
00116 QTextStream(&xmlNewFile) << newXmlFileContent;
00117 xmlFile.remove();
00118 xmlNewFile.rename(helper_stationlistXmlFilepath());
00119
00120
00121 KMessageBox::saveDontShowAgainYesNo("askForKradioripperImport", KMessageBox::No);
00122 KConfigGroup group_import(settings_general::self()->config(), "kradioripperimport");
00123 group_import.writeEntry<bool>("importRequested", false);
00124 group_import.sync();
00125 settings_general::self()->writeConfig();
00126 settings_general::self()->readConfig();
00127 }
00128
00129 void importKradioripperSettings::askForImport(bool use_dontAskAgainName)
00130 {
00131 QString dontAskAgainName;
00132 if (use_dontAskAgainName) {
00133 dontAskAgainName = "askForKradioripperImport";
00134 }
00135 if (KMessageBox::questionYesNo(0,
00136 i18nc("@info", "<para>Do you want to import KRadioRipper "
00137 "settings?</para><para><note>The import "
00138 "will be performed when you start "
00139 "KStreamRipper the next time.</note></para>"),
00140 i18nc("@title:window", "Import KRadioRipper settings"),
00141 KStandardGuiItem::yes(),
00142 KStandardGuiItem::no(),
00143 dontAskAgainName) == KMessageBox::Yes) {
00144 KConfigGroup group_import(settings_general::self()->config(), "kradioripperimport");
00145 group_import.writeEntry<bool>("importRequested", true);
00146 group_import.sync();
00147 settings_general::self()->writeConfig();
00148 settings_general::self()->readConfig();
00149 };
00150 }