Mixxx

/home/maxime/Projets/Mixxx/1.10/mixxx/src/upgrade.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           upgrade.cpp  -  description
00003                              -------------------
00004     begin                : Fri Mar 13 2009
00005     copyright            : (C) 2009 by Sean M. Pappalardo
00006     email                : pegasus@c64.org
00007 ***************************************************************************/
00008 
00009 /***************************************************************************
00010 *                                                                         *
00011 *   This program is free software; you can redistribute it and/or modify  *
00012 *   it under the terms of the GNU General Public License as published by  *
00013 *   the Free Software Foundation; either version 2 of the License, or     *
00014 *   (at your option) any later version.                                   *
00015 *                                                                         *
00016 ***************************************************************************/
00017 
00018 #include <QtCore>
00019 #include "defs_version.h"
00020 
00021 #include "configobject.h"
00022 #include "upgrade.h"
00023 
00024 Upgrade::Upgrade()
00025 {
00026     m_bFirstRun = false;
00027     m_bUpgraded = false;
00028 }
00029 
00030 Upgrade::~Upgrade()
00031 {
00032 
00033 }
00034 
00035 // We return the ConfigObject here because we have to make changes to the
00036 // configuration and the location of the file may change between releases.
00037 ConfigObject<ConfigValue>* Upgrade::versionUpgrade() {
00038 
00039 /*  Pre-1.7.0:
00040 *
00041 *   Since we didn't store version numbers in the config file prior to 1.7.0,
00042 *   we check to see if the user is upgrading if his config files are in the old location,
00043 *   since we moved them in 1.7.0. This code takes care of moving them.
00044 */
00045 
00046     QString oldLocation = QDir::homePath().append("/%1");
00047 #ifdef __WINDOWS__
00048     QFileInfo* pre170Config = new QFileInfo(oldLocation.arg("mixxx.cfg"));
00049 #else
00050     QFileInfo* pre170Config = new QFileInfo(oldLocation.arg(".mixxx.cfg"));
00051 #endif
00052 
00053     if (pre170Config->exists()) {
00054 
00055         // Move the files to their new location
00056         QString newLocation = QDir::homePath().append("/").append(SETTINGS_PATH);
00057         
00058         if (!QDir(newLocation).exists()) {
00059             qDebug() << "Creating new settings directory" << newLocation;
00060             QDir().mkpath(newLocation);
00061         }
00062         
00063         newLocation.append("%1");
00064         QString errorText = "Error moving your %1 file %2 to the new location %3: \n";
00065 
00066 #ifdef __WINDOWS__
00067         QString oldFilePath = oldLocation.arg("mixxxtrack.xml");
00068 #else
00069         QString oldFilePath = oldLocation.arg(".mixxxtrack.xml");
00070 #endif
00071 
00072         QString newFilePath = newLocation.arg("mixxxtrack.xml");
00073         QFile* oldFile = new QFile(oldFilePath);
00074         if (oldFile->exists()) {
00075             if (oldFile->copy(newFilePath)) {
00076                 oldFile->remove();
00077                 m_bUpgraded = true;
00078             }
00079             else {
00080                 if (oldFile->error()==14) qDebug() << errorText.arg("library", oldFilePath, newFilePath) << "The destination file already exists.";
00081                 else qDebug() << errorText.arg("library", oldFilePath, newFilePath) << "Error #" << oldFile->error();
00082             }
00083         }
00084         delete oldFile;
00085         
00086 #ifdef __WINDOWS__
00087         oldFilePath = oldLocation.arg("mixxxbpmschemes.xml");
00088 #else
00089         oldFilePath = oldLocation.arg(".mixxxbpmscheme.xml");
00090 #endif
00091         newFilePath = newLocation.arg("mixxxbpmscheme.xml");
00092         oldFile = new QFile(oldFilePath);
00093         if (oldFile->exists()) {
00094             if (oldFile->copy(newFilePath))
00095                 oldFile->remove();
00096             else {
00097                 if (oldFile->error()==14) qDebug() << errorText.arg("settings", oldFilePath, newFilePath) << "The destination file already exists.";
00098                 else qDebug() << errorText.arg("settings", oldFilePath, newFilePath) << "Error #" << oldFile->error();
00099             }
00100         }
00101         delete oldFile;
00102 #ifdef __WINDOWS__
00103         oldFilePath = oldLocation.arg("MixxxMIDIBindings.xml");
00104 #else
00105         oldFilePath = oldLocation.arg(".MixxxMIDIBindings.xml");
00106 #endif
00107         newFilePath = newLocation.arg("MixxxMIDIBindings.xml");
00108         oldFile = new QFile(oldFilePath);
00109         if (oldFile->exists()) {
00110             qWarning() << "The MIDI mapping file format has changed in this version of Mixxx. You will need to reconfigure your MIDI controller. See the Wiki for full details on the new format.";
00111             if (oldFile->copy(newFilePath))
00112                 oldFile->remove();
00113             else {
00114                 if (oldFile->error()==14) qDebug() << errorText.arg("MIDI mapping", oldFilePath, newFilePath) << "The destination file already exists.";
00115                 else qDebug() << errorText.arg("MIDI mapping", oldFilePath, newFilePath) << "Error #" << oldFile->error();
00116             }
00117         }
00118         // Tidy up
00119         delete oldFile;
00120 
00121         QFile::remove(oldLocation.arg(".MixxxMIDIDevice.xml")); // Obsolete file, so just delete it
00122 
00123 #ifdef __WINDOWS__
00124         oldFilePath = oldLocation.arg("mixxx.cfg");
00125 #else
00126         oldFilePath = oldLocation.arg(".mixxx.cfg");
00127 #endif
00128         newFilePath = newLocation.arg(SETTINGS_FILE);
00129         oldFile = new QFile(oldFilePath);
00130         if (oldFile->copy(newFilePath))
00131             oldFile->remove();
00132         else {
00133                 if (oldFile->error()==14) qDebug() << errorText.arg("configuration", oldFilePath, newFilePath) << "The destination file already exists.";
00134                 else qDebug() << errorText.arg("configuration", oldFilePath, newFilePath) << "Error #" << oldFile->error();
00135             }
00136         delete oldFile;
00137         
00138     }
00139     // Tidy up
00140     delete pre170Config;
00141     // End pre-1.7.0 code
00142     
00143     
00144 /***************************************************************************
00145 *                           Post-1.7.0 upgrade code
00146 *
00147 *   Add entries to the IF ladder below if anything needs to change from the
00148 *   previous to the current version. This allows for incremental upgrades
00149 *   incase a user upgrades from a few versions prior.
00150 ****************************************************************************/
00151 
00152     // Read the config file from home directory
00153     ConfigObject<ConfigValue> *config = new ConfigObject<ConfigValue>(QDir::homePath().append("/").append(SETTINGS_PATH).append(SETTINGS_FILE));
00154 
00155     QString configVersion = config->getValueString(ConfigKey("[Config]","Version"));
00156 
00157     if (configVersion.isEmpty()) {
00158 
00159 #ifdef __APPLE__
00160         qDebug() << "Config version is empty, trying to read pre-1.9.0 config";
00161         //Try to read the config from the pre-1.9.0 final directory on OS X (we moved it in 1.9.0 final)
00162         QFile* oldFile = new QFile(QDir::homePath().append("/").append(".mixxx/mixxx.cfg"));
00163         if (oldFile->exists()) {
00164             qDebug() << "Found pre-1.9.0 config for OS X";
00165             config = new ConfigObject<ConfigValue>(QDir::homePath().append("/").append(".mixxx/mixxx.cfg"));
00166             //Note: We changed SETTINGS_PATH in 1.9.0 final on OS X so it must be hardcoded to ".mixxx" here for legacy.
00167             configVersion = config->getValueString(ConfigKey("[Config]","Version"));
00168             delete oldFile;
00169         }
00170         else {
00171 #endif
00172             //This must have been the first run... right? :)
00173             qDebug() << "No version number in configuration file. Setting to" << VERSION;
00174             config->set(ConfigKey("[Config]","Version"), ConfigValue(VERSION));
00175             m_bFirstRun = true;
00176             return config;
00177 #ifdef __APPLE__
00178         }
00179 #endif
00180     }
00181     
00182     // If it's already current, stop here
00183     if (configVersion == VERSION) {
00184         qDebug() << "Configuration file is at the current version" << VERSION;
00185         return config;
00186     }
00187 
00188     // Allows for incremental upgrades incase someone upgrades from a few versions prior
00189     // (I wish we could do a switch on a QString.)
00190     /*
00191     // Examples, since we didn't store the version number prior to v1.7.0
00192     if (configVersion.startsWith("1.6.0")) {
00193         qDebug() << "Upgrading from v1.6.0 to 1.6.1...";
00194         // Upgrade tasks go here
00195         configVersion = "1.6.1";
00196         config->set(ConfigKey("[Config]","Version"), ConfigValue("1.6.1"));
00197     }
00198     if (configVersion.startsWith("1.6.1")) {
00199         qDebug() << "Upgrading from v1.6.1 to 1.7.0...";
00200         // Upgrade tasks go here
00201         configVersion = "1.7.0";
00202         config->set(ConfigKey("[Config]","Version"), ConfigValue("1.7.0"));
00203     }
00204     */
00205 
00206     //We use the following blocks to detect if this is the first time
00207     //you've run the latest version of Mixxx. This lets us show
00208     //the promo tracks stats agreement stuff for all users that are
00209     //upgrading Mixxx.
00210     
00211     if (configVersion.startsWith("1.7")) {
00212         qDebug() << "Upgrading from v1.7.x to" << VERSION <<"...";
00213         // Upgrade tasks go here
00214         // Nothing to change, really
00215     }
00216 
00217     if (configVersion.startsWith("1.8.0~beta1") || 
00218         configVersion.startsWith("1.8.0~beta2")) {
00219         qDebug() << "Upgrading from v1.8.0~beta to" << VERSION <<"...";
00220         // Upgrade tasks go here
00221     }
00222     if (configVersion.startsWith("1.8") || configVersion.startsWith("1.9.0beta1")) {
00223         qDebug() << "Upgrading from" << configVersion << "to" << VERSION <<"...";
00224         // Upgrade tasks go here
00225 #ifdef __APPLE__
00226         QString OSXLocation180 = QDir::homePath().append("/").append(".mixxx");
00227         QString OSXLocation190 = QDir::homePath().append("/").append(SETTINGS_PATH);
00228         QDir newOSXDir(OSXLocation190);
00229         newOSXDir.mkpath(OSXLocation190);
00230 
00231         QList<QPair<QString, QString> > dirsToMove;
00232         dirsToMove.push_back(QPair<QString, QString>(OSXLocation180, OSXLocation190));
00233         dirsToMove.push_back(QPair<QString, QString>(OSXLocation180 + "/midi", OSXLocation190 + "midi"));
00234         dirsToMove.push_back(QPair<QString, QString>(OSXLocation180 + "/presets", OSXLocation190 + "presets"));
00235 
00236         QListIterator<QPair<QString, QString> > dirIt(dirsToMove);
00237         QPair<QString, QString> curPair;
00238         while (dirIt.hasNext())
00239         {
00240             curPair = dirIt.next();
00241             qDebug() << "Moving" << curPair.first << "to" << curPair.second;
00242             QDir oldSubDir(curPair.first);
00243             QDir newSubDir(curPair.second);
00244             newSubDir.mkpath(curPair.second); //Create the new destination directory
00245             
00246             QStringList contents = oldSubDir.entryList(QDir::Files | QDir::NoDotAndDotDot);
00247             QStringListIterator it(contents);
00248             QString cur;
00249             //Iterate over all the files in the source directory and copy them to the dest dir.
00250             while (it.hasNext())
00251             {
00252                 cur = it.next();
00253                 QString src = curPair.first + "/" + cur;
00254                 QString dest = curPair.second + "/" + cur;
00255                 qDebug() << "Copying" << src << "to" << dest;
00256                 if (!QFile::copy(src, dest))
00257                 {
00258                     qDebug() << "Failed to move file during upgrade.";
00259                 }
00260             }
00261 
00262             //Rename the old directory.
00263             newOSXDir.rename(OSXLocation180, OSXLocation180+ "-1.8");
00264         }
00265         //Reload the configuration file from the new location. 
00266         //(We want to make sure we save to the new location...)
00267         config = new ConfigObject<ConfigValue>(QDir::homePath().append("/").append(SETTINGS_PATH).append(SETTINGS_FILE));
00268 #endif
00269     }
00270     // For the next release
00271     /*
00272     if (configVersion.startsWith("1.8.0")) {
00273         qDebug() << "Upgrading from v1.8.0 to" << VERSION <<"...";
00274         // Upgrade tasks go here, if any
00275         configVersion = VERSION;
00276         config->set(ConfigKey("[Config]","Version"), ConfigValue(VERSION));
00277     }
00278     */
00279 
00280     configVersion = VERSION;
00281     m_bUpgraded = true;
00282     config->set(ConfigKey("[Config]","Version"), ConfigValue(VERSION));
00283 
00284     if (configVersion == VERSION) qDebug() << "Configuration file is now at the current version" << VERSION;
00285     else {
00286         /* Way too verbose, this confuses the hell out of Linux users when they see this:
00287         qWarning() << "Configuration file is at version" << configVersion
00288                    << "and I don't know how to upgrade it to the current" << VERSION
00289                    << "\n   (That means a function to do this needs to be added to upgrade.cpp.)"
00290                    << "\n-> Leaving the configuration file version as-is.";
00291         */
00292     }
00293 
00294     return config;
00295 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines