importkradiorippersettings.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2008-2010  Lukas Sommer < SommerLuk at gmail dot com >
00003 
00004     This program is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU General Public License as
00006     published by the Free Software Foundation; either version 2 of
00007     the License or (at your option) version 3 or any later version
00008     accepted by the membership of KDE e.V. (or its successor approved
00009     by the membership of KDE e.V.), which shall act as a proxy
00010     defined in Section 14 of version 3 of the license.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
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   // variables
00049   QFileInfo info;
00050   QString returnValue;
00051 
00052   // code
00053   KTemporaryFile newConfigFile;
00054   // Maybe directories are missing in our desired path - locateLocal() will
00055   // create them if nesessary...
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   // copy config file
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   // reload config file
00088   settings_general::self()->readConfig();
00089 
00090   // convert stream list
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   // QTextStream will use the default locale (for example 8859-1), and not Unicode.
00112   // However, as our XML is plain ASCII, this doesn't matter.
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   // adapt settings for next startup
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 }

doxygen