/src/libtomahawk/TomahawkSettings.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 1764 lines · 1323 code · 386 blank · 55 comment · 123 complexity · dfe280214602c850b81f1dd9c9da8477 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2015, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4. * Copyright 2010-2011 Leo Franchi <lfranchi@kde.org>
  5. * Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
  6. *
  7. * Tomahawk is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Tomahawk is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "TomahawkSettings.h"
  21. #include "collection/Collection.h"
  22. #include "database/DatabaseCommand_UpdateSearchIndex.h"
  23. #include "database/Database.h"
  24. #include "database/fuzzyindex/DatabaseFuzzyIndex.h"
  25. #include "infosystem/InfoSystemCache.h"
  26. #include "playlist/PlaylistUpdaterInterface.h"
  27. #include "utils/Logger.h"
  28. #include "utils/Json.h"
  29. #include "utils/TomahawkUtils.h"
  30. #include "PlaylistEntry.h"
  31. #include "PlaylistInterface.h"
  32. #include "Source.h"
  33. #include <qt5keychain/keychain.h>
  34. #include <QStandardPaths>
  35. #include <QDir>
  36. using namespace Tomahawk;
  37. TomahawkSettings* TomahawkSettings::s_instance = 0;
  38. inline QDataStream&
  39. operator<<(QDataStream& out, const SerializedUpdaters& updaters)
  40. {
  41. out << TOMAHAWK_SETTINGS_VERSION;
  42. out << (quint32)updaters.count();
  43. SerializedUpdaters::const_iterator iter = updaters.begin();
  44. int count = 0;
  45. for ( ; iter != updaters.end(); ++iter )
  46. {
  47. out << iter.key() << iter->type << iter->customData;
  48. count++;
  49. }
  50. Q_ASSERT( count == updaters.count() );
  51. return out;
  52. }
  53. inline QDataStream&
  54. operator>>(QDataStream& in, SerializedUpdaters& updaters)
  55. {
  56. quint32 count = 0, version = 0;
  57. in >> version;
  58. in >> count;
  59. for ( uint i = 0; i < count; i++ )
  60. {
  61. QString key, type;
  62. QVariantHash customData;
  63. in >> key;
  64. in >> type;
  65. in >> customData;
  66. updaters.insert( key, SerializedUpdater( type, customData ) );
  67. }
  68. return in;
  69. }
  70. TomahawkSettings*
  71. TomahawkSettings::instance()
  72. {
  73. return s_instance;
  74. }
  75. TomahawkSettings::TomahawkSettings( QObject* parent )
  76. : QSettings( parent )
  77. {
  78. s_instance = this;
  79. #if !(defined(Q_OS_MAC) && defined(Q_OS_WIN))
  80. QFile file( fileName() );
  81. file.setPermissions( file.permissions() & ~( QFile::ReadGroup | QFile::WriteGroup | QFile::ExeGroup | QFile::ReadOther | QFile::WriteOther | QFile::ExeOther ) );
  82. #endif
  83. if ( !contains( "configversion" ) )
  84. {
  85. setValue( "configversion", TOMAHAWK_SETTINGS_VERSION );
  86. doInitialSetup();
  87. }
  88. else if ( value( "configversion" ).toUInt() != TOMAHAWK_SETTINGS_VERSION )
  89. {
  90. qDebug() << "Config version outdated, old:" << value( "configversion" ).toUInt()
  91. << "new:" << TOMAHAWK_SETTINGS_VERSION
  92. << "Doing upgrade, if any, and backing up";
  93. // QString newname = QString( "%1.v%2" ).arg( dbname ).arg( version );
  94. if ( format() == IniFormat ||
  95. ( format() == NativeFormat
  96. #ifdef Q_OS_WIN
  97. && false
  98. #endif
  99. ) )
  100. {
  101. qDebug() << "Backing up old ini-style config file";
  102. const QString path = fileName();
  103. const QString newname = path + QString( ".v%1" ).arg( value( "configversion" ).toString() );
  104. QFile::copy( path, newname );
  105. }
  106. int current = value( "configversion" ).toUInt();
  107. while ( current < TOMAHAWK_SETTINGS_VERSION )
  108. {
  109. doUpgrade( current, current + 1 );
  110. current++;
  111. }
  112. // insert upgrade code here as required
  113. setValue( "configversion", TOMAHAWK_SETTINGS_VERSION );
  114. }
  115. // Ensure last.fm and spotify accounts always exist
  116. QString spotifyAcct, lastfmAcct;
  117. foreach ( const QString& acct, value( "accounts/allaccounts" ).toStringList() )
  118. {
  119. if ( acct.startsWith( "lastfmaccount_" ) )
  120. lastfmAcct = acct;
  121. else if ( acct.startsWith( "spotifyaccount_" ) )
  122. spotifyAcct = acct;
  123. }
  124. if ( spotifyAcct.isEmpty() )
  125. createSpotifyAccount();
  126. if ( lastfmAcct.isEmpty() )
  127. createLastFmAccount();
  128. }
  129. TomahawkSettings::~TomahawkSettings()
  130. {
  131. s_instance = 0;
  132. }
  133. void
  134. TomahawkSettings::doInitialSetup()
  135. {
  136. // by default we add a local network resolver
  137. addAccount( "sipzeroconf_autocreated" );
  138. createLastFmAccount();
  139. createSpotifyAccount();
  140. }
  141. void
  142. TomahawkSettings::createLastFmAccount()
  143. {
  144. // Add a last.fm account for scrobbling and infosystem
  145. const QString accountKey = QString( "lastfmaccount_%1" ).arg( QUuid::createUuid().toString().mid( 1, 8 ) );
  146. addAccount( accountKey );
  147. beginGroup( "accounts/" + accountKey );
  148. setValue( "enabled", false );
  149. setValue( "autoconnect", true );
  150. setValue( "types", QStringList() << "ResolverType" << "StatusPushType" );
  151. endGroup();
  152. QStringList allAccounts = value( "accounts/allaccounts" ).toStringList();
  153. allAccounts << accountKey;
  154. setValue( "accounts/allaccounts", allAccounts );
  155. }
  156. void
  157. TomahawkSettings::createSpotifyAccount()
  158. {
  159. const QString accountKey = QString( "spotifyaccount_%1" ).arg( QUuid::createUuid().toString().mid( 1, 8 ) );
  160. beginGroup( "accounts/" + accountKey );
  161. setValue( "enabled", false );
  162. setValue( "types", QStringList() << "ResolverType" );
  163. setValue( "configuration", QVariantHash() );
  164. setValue( "accountfriendlyname", "Spotify" );
  165. endGroup();
  166. QStringList allAccounts = value( "accounts/allaccounts" ).toStringList();
  167. allAccounts << accountKey;
  168. setValue( "accounts/allaccounts", allAccounts );
  169. }
  170. void
  171. TomahawkSettings::doUpgrade( int oldVersion, int newVersion )
  172. {
  173. Q_UNUSED( newVersion );
  174. if ( oldVersion == 1 )
  175. {
  176. qDebug() << "Migrating config from version 1 to 2: script resolver config name";
  177. if( contains( "script/resolvers" ) ) {
  178. setValue( "script/loadedresolvers", value( "script/resolvers" ) );
  179. remove( "script/resolvers" );
  180. }
  181. }
  182. else if ( oldVersion == 2 )
  183. {
  184. qDebug() << "Migrating config from version 2 to 3: Converting jabber and twitter accounts to new SIP Factory approach";
  185. // migrate old accounts to new system. only jabber and twitter, and max one each. create a new plugin for each if needed
  186. // not pretty as we hardcode a plugin id and assume that we know how the config layout is, but hey, this is migration after all
  187. if ( contains( "jabber/username" ) && contains( "jabber/password" ) )
  188. {
  189. QString sipName = "sipjabber";
  190. if ( value( "jabber/username" ).toString().contains( "@gmail" ) )
  191. sipName = "sipgoogle";
  192. setValue( QString( "%1_legacy/username" ).arg( sipName ), value( "jabber/username" ) );
  193. setValue( QString( "%1_legacy/password" ).arg( sipName ), value( "jabber/password" ) );
  194. setValue( QString( "%1_legacy/autoconnect" ).arg( sipName ), value( "jabber/autoconnect" ) );
  195. setValue( QString( "%1_legacy/port" ).arg( sipName ), value( "jabber/port" ) );
  196. setValue( QString( "%1_legacy/server" ).arg( sipName ), value( "jabber/server" ) );
  197. addSipPlugin( QString( "%1_legacy" ).arg( sipName ) );
  198. remove( "jabber/username" );
  199. remove( "jabber/password" );
  200. remove( "jabber/autoconnect" );
  201. remove( "jabber/server" );
  202. remove( "jabber/port" );
  203. }
  204. if ( contains( "twitter/ScreenName" ) && contains( "twitter/OAuthToken" ) )
  205. {
  206. setValue( "siptwitter_legacy/ScreenName", value( "twitter/ScreenName" ) );
  207. setValue( "siptwitter_legacy/OAuthToken", value( "twitter/OAuthToken" ) );
  208. setValue( "siptwitter_legacy/OAuthTokenSecret", value( "twitter/OAuthTokenSecret" ) );
  209. setValue( "siptwitter_legacy/CachedFriendsSinceID", value( "twitter/CachedFriendsSinceID" ) );
  210. setValue( "siptwitter_legacy/CachedMentionsSinceID", value( "twitter/CachedMentionsSinceID" ) );
  211. setValue( "siptwitter_legacy/CachedDirectMessagesSinceID", value( "twitter/CachedDirectMessagesSinceID" ) );
  212. setValue( "siptwitter_legacy/CachedPeers", value( "twitter/CachedPeers" ) );
  213. setValue( "siptwitter_legacy/AutoConnect", value( "jabber/autoconnect" ) );
  214. addSipPlugin( "siptwitter_legacy" );
  215. remove( "twitter/ScreenName" );
  216. remove( "twitter/OAuthToken" );
  217. remove( "twitter/OAuthTokenSecret" );
  218. remove( "twitter/CachedFriendsSinceID" );
  219. remove( "twitter/CachedMentionsSinceID" );
  220. remove( "twitter/CachedDirectMessagesSinceID" );
  221. }
  222. // create a zeroconf plugin too
  223. addSipPlugin( "sipzeroconf_legacy" );
  224. }
  225. else if ( oldVersion == 3 )
  226. {
  227. if ( contains( "script/atticaresolverstates" ) )
  228. {
  229. // Do messy binary upgrade. remove attica resolvers :(
  230. setValue( "script/atticaresolverstates", QVariant() );
  231. QDir resolverDir = TomahawkUtils::appDataDir();
  232. if ( !resolverDir.cd( QString( "atticaresolvers" ) ) )
  233. return;
  234. QStringList toremove;
  235. QStringList resolvers = resolverDir.entryList( QDir::Dirs | QDir::NoDotAndDotDot );
  236. QStringList listedResolvers = value( "script/resolvers" ).toStringList();
  237. QStringList enabledResolvers = value( "script/loadedresolvers" ).toStringList();
  238. foreach ( const QString& resolver, resolvers )
  239. {
  240. foreach ( const QString& r, listedResolvers )
  241. {
  242. if ( r.contains( resolver ) )
  243. {
  244. tDebug() << "Deleting listed resolver:" << r;
  245. listedResolvers.removeAll( r );
  246. }
  247. }
  248. foreach ( const QString& r, enabledResolvers )
  249. {
  250. if ( r.contains( resolver ) )
  251. {
  252. tDebug() << "Deleting enabled resolver:" << r;
  253. enabledResolvers.removeAll( r );
  254. }
  255. }
  256. }
  257. setValue( "script/resolvers", listedResolvers );
  258. setValue( "script/loadedresolvers", enabledResolvers );
  259. tDebug() << "UPGRADING AND DELETING:" << resolverDir.absolutePath();
  260. TomahawkUtils::removeDirectory( resolverDir.absolutePath() );
  261. }
  262. }
  263. else if ( oldVersion == 4 || oldVersion == 5 )
  264. {
  265. // 0.3.0 contained a bug which prevent indexing local files. Force a reindex.
  266. Tomahawk::DatabaseFuzzyIndex::wipeIndex();
  267. updateIndex();
  268. }
  269. else if ( oldVersion == 6 )
  270. {
  271. // Migrate to accounts from sipplugins.
  272. // collect old connected and enabled sip plugins
  273. const QStringList allSip = sipPlugins();
  274. const QStringList enabledSip = enabledSipPlugins();
  275. QStringList accounts;
  276. foreach ( const QString& sipPlugin, allSip )
  277. {
  278. const QStringList parts = sipPlugin.split( "_" );
  279. Q_ASSERT( parts.size() == 2 );
  280. const QString pluginName = parts[ 0 ];
  281. const QString pluginId = parts[ 1 ];
  282. // new key, <plugin>account_<id>
  283. QString rawpluginname = pluginName;
  284. rawpluginname.replace( "sip", "" );
  285. if ( rawpluginname.contains( "jabber" ) )
  286. rawpluginname.replace( "jabber", "xmpp" );
  287. QString accountKey = QString( "%1account_%2" ).arg( rawpluginname ).arg( pluginId );
  288. if ( pluginName == "sipjabber" || pluginName == "sipgoogle" )
  289. {
  290. QVariantHash credentials;
  291. credentials[ "username" ] = value( sipPlugin + "/username" );
  292. credentials[ "password" ] = value( sipPlugin + "/password" );
  293. QVariantHash configuration;
  294. configuration[ "port" ] = value( sipPlugin + "/port" );
  295. configuration[ "server" ] = value( sipPlugin + "/server" );
  296. setValue( QString( "accounts/%1/credentials" ).arg( accountKey ), credentials );
  297. setValue( QString( "accounts/%1/configuration" ).arg( accountKey ), configuration );
  298. setValue( QString( "accounts/%1/accountfriendlyname" ).arg( accountKey ), value( sipPlugin + "/username" ) );
  299. }
  300. else if ( pluginName == "siptwitter" )
  301. {
  302. // Only port twitter plugin if there's a valid twitter config
  303. if ( value( sipPlugin + "/oauthtokensecret" ).toString().isEmpty() &&
  304. value( sipPlugin + "/oauthtoken" ).toString().isEmpty() &&
  305. value( sipPlugin + "/screenname" ).toString().isEmpty() )
  306. continue;
  307. QVariantMap credentials;
  308. credentials[ "oauthtoken" ] = value( sipPlugin + "/oauthtoken" );
  309. credentials[ "oauthtokensecret" ] = value( sipPlugin + "/oauthtokensecret" );
  310. credentials[ "username" ] = value( sipPlugin + "/screenname" );
  311. QVariantHash configuration;
  312. configuration[ "cachedfriendssinceid" ] = value( sipPlugin + "/cachedfriendssinceid" );
  313. configuration[ "cacheddirectmessagessinceid" ] = value( sipPlugin + "/cacheddirectmessagessinceid" );
  314. configuration[ "cachedpeers" ] = value( sipPlugin + "/cachedpeers" );
  315. qDebug() << "FOUND CACHED PEERS:" << value( sipPlugin + "/cachedpeers" ).toHash();
  316. configuration[ "cachedmentionssinceid" ] = value( sipPlugin + "/cachedmentionssinceid" );
  317. configuration[ "saveddbid" ] = value( sipPlugin + "/saveddbid" );
  318. setValue( QString( "accounts/%1/credentials" ).arg( accountKey ), credentials );
  319. setValue( QString( "accounts/%1/configuration" ).arg( accountKey ), configuration );
  320. setValue( QString( "accounts/%1/accountfriendlyname" ).arg( accountKey ), "@" + value( sipPlugin + "/screenname" ).toString() );
  321. sync();
  322. }
  323. else if ( pluginName == "sipzeroconf" )
  324. {
  325. setValue( QString( "accounts/%1/accountfriendlyname" ).arg( accountKey ), tr( "Local Network" ) );
  326. }
  327. beginGroup( "accounts/" + accountKey );
  328. setValue( "enabled", enabledSip.contains( sipPlugin ) == true );
  329. setValue( "autoconnect", true );
  330. setValue( "acl", QVariantHash() );
  331. setValue( "types", QStringList() << "SipType" );
  332. endGroup();
  333. accounts << accountKey;
  334. remove( sipPlugin );
  335. }
  336. remove( "sip" );
  337. // Migrate all resolvers from old resolvers settings to new accounts system
  338. const QStringList allResolvers = value( "script/resolvers" ).toStringList();
  339. const QStringList enabledResolvers = value( "script/loadedresolvers" ).toStringList();
  340. foreach ( const QString& resolver, allResolvers )
  341. {
  342. // We handle last.fm resolvers differently.
  343. if ( resolver.contains( "lastfm" ) )
  344. continue;
  345. const QString accountKey = QString( "resolveraccount_%1" ).arg( QUuid::createUuid().toString().mid( 1, 8 ) );
  346. accounts << accountKey;
  347. beginGroup( "accounts/" + accountKey );
  348. setValue( "accountfriendlyname", resolver );
  349. setValue( "enabled", enabledResolvers.contains( resolver ) == true );
  350. setValue( "autoconnect", true );
  351. setValue( "types", QStringList() << "ResolverType" );
  352. QVariantHash configuration;
  353. configuration[ "path" ] = resolver;
  354. // reasonably ugly check for attica resolvers
  355. if ( resolver.contains( "atticaresolvers" ) && resolver.contains( "code" ) )
  356. {
  357. setValue( "atticaresolver", true );
  358. QFileInfo info( resolver );
  359. configuration[ "atticaId" ] = info.baseName();
  360. }
  361. setValue( "configuration", configuration );
  362. endGroup();
  363. }
  364. // Add a Last.Fm account since we now moved the infoplugin into the account
  365. const QString accountKey = QString( "lastfmaccount_%1" ).arg( QUuid::createUuid().toString().mid( 1, 8 ) );
  366. accounts << accountKey;
  367. const QString lfmUsername = value( "lastfm/username" ).toString();
  368. const QString lfmPassword = value( "lastfm/password" ).toString();
  369. const bool scrobble = value( "lastfm/enablescrobbling", false ).toBool();
  370. beginGroup( "accounts/" + accountKey );
  371. bool hasLastFmEnabled = false;
  372. foreach ( const QString& r, enabledResolvers )
  373. {
  374. if ( r.contains( "lastfm" ) )
  375. {
  376. hasLastFmEnabled = true;
  377. break;
  378. }
  379. }
  380. setValue( "enabled", hasLastFmEnabled );
  381. setValue( "autoconnect", true );
  382. setValue( "types", QStringList() << "ResolverType" << "StatusPushType" );
  383. QVariantMap credentials;
  384. credentials[ "username" ] = lfmUsername;
  385. credentials[ "password" ] = lfmPassword;
  386. credentials[ "session" ] = value( "lastfm/session" ).toString();
  387. setValue( "credentials", credentials );
  388. QVariantHash configuration;
  389. configuration[ "scrobble" ] = scrobble;
  390. setValue( "configuration", configuration );
  391. endGroup();
  392. remove( "lastfm" );
  393. remove( "script/resolvers" );
  394. remove( "script/loadedresolvers" );
  395. setValue( "accounts/allaccounts", accounts );
  396. }
  397. else if ( oldVersion == 7 )
  398. {
  399. // Upgrade spotify resolver to standalone account, if one exists
  400. beginGroup( "accounts" );
  401. QStringList allAccounts = value( "allaccounts" ).toStringList();
  402. foreach ( const QString& account, allAccounts )
  403. {
  404. if ( account.startsWith( "resolveraccount_" ) && value( QString( "%1/accountfriendlyname" ).arg( account ) ).toString().endsWith( "spotify_tomahawkresolver" ) )
  405. {
  406. // This is a spotify resolver, convert!
  407. const QVariantHash configuration = value( QString( "%1/configuration" ).arg( account ) ).toHash();
  408. const bool enabled = value( QString( "%1/enabled" ).arg( account ) ).toBool();
  409. const bool autoconnect = value( QString( "%1/autoconnect" ).arg( account ) ).toBool();
  410. qDebug() << "Migrating Spotify resolver from legacy resolver type, path is:" << configuration[ "path" ].toString();
  411. remove( account );
  412. // Create new account
  413. QString newAccount = account;
  414. newAccount.replace( "resolveraccount_", "spotifyaccount_" );
  415. beginGroup( newAccount );
  416. setValue( "enabled", enabled );
  417. setValue( "autoconnect", autoconnect );
  418. setValue( "types", QStringList() << "ResolverType" );
  419. setValue( "accountfriendlyname", "Spotify" );
  420. setValue( "configuration", configuration );
  421. endGroup();
  422. allAccounts.replace( allAccounts.indexOf( account ), newAccount );
  423. }
  424. }
  425. setValue( "allaccounts", allAccounts );
  426. endGroup();
  427. }
  428. else if ( oldVersion == 8 )
  429. {
  430. // Some users got duplicate accounts for some reason, so make them unique if we can
  431. QSet< QString > uniqueFriendlyNames;
  432. beginGroup("accounts");
  433. const QStringList accounts = childGroups();
  434. QStringList allAccounts = value( "allaccounts" ).toStringList();
  435. // qDebug() << "Got accounts to migrate:" << accounts;
  436. foreach ( const QString& account, accounts )
  437. {
  438. if ( !allAccounts.contains( account ) ) // orphan
  439. {
  440. qDebug() << "Found account not in allaccounts list!" << account << "is a dup!";
  441. remove( account );
  442. continue;
  443. }
  444. const QString friendlyName = value( QString( "%1/accountfriendlyname" ).arg( account ) ).toString();
  445. if ( !uniqueFriendlyNames.contains( friendlyName ) )
  446. {
  447. uniqueFriendlyNames.insert( friendlyName );
  448. continue;
  449. }
  450. else
  451. {
  452. // Duplicate..?
  453. qDebug() << "Found duplicate account friendly name:" << account << friendlyName << "is a dup!";
  454. remove( account );
  455. allAccounts.removeAll( account );
  456. }
  457. }
  458. qDebug() << "Ended up with all accounts list:" << allAccounts << "and all accounts:" << childGroups();
  459. setValue( "allaccounts", allAccounts );
  460. endGroup();
  461. }
  462. else if ( oldVersion == 9 )
  463. {
  464. // Upgrade single-updater-per-playlist to list-per-playlist
  465. beginGroup( "playlistupdaters" );
  466. const QStringList playlists = childGroups();
  467. SerializedUpdaters updaters;
  468. foreach ( const QString& playlist, playlists )
  469. {
  470. beginGroup( playlist );
  471. const QString type = value( "type" ).toString();
  472. QVariantHash extraData;
  473. foreach ( const QString& key, childKeys() )
  474. {
  475. if ( key == "type" )
  476. continue;
  477. extraData[ key ] = value( key );
  478. }
  479. updaters.insert( playlist, SerializedUpdater( type, extraData ) );
  480. endGroup();
  481. }
  482. endGroup();
  483. remove( "playlistupdaters" );
  484. setValue( "playlists/updaters", QVariant::fromValue< SerializedUpdaters >( updaters ) );
  485. }
  486. else if ( oldVersion == 11 )
  487. {
  488. // If the user doesn't have a spotify account, create one, since now it
  489. // is like the last.fm account and always exists
  490. QStringList allAccounts = value( "accounts/allaccounts" ).toStringList();
  491. QString acct;
  492. foreach ( const QString& account, allAccounts )
  493. {
  494. if ( account.startsWith( "spotifyaccount_" ) )
  495. {
  496. acct = account;
  497. break;
  498. }
  499. }
  500. if ( !acct.isEmpty() )
  501. {
  502. beginGroup( "accounts/" + acct );
  503. QVariantHash conf = value( "configuration" ).toHash();
  504. foreach ( const QString& key, conf.keys() )
  505. qDebug() << key << conf[ key ].toString();
  506. endGroup();
  507. }
  508. else
  509. {
  510. createSpotifyAccount();
  511. }
  512. }
  513. else if ( oldVersion == 12 )
  514. {
  515. // Force attica resolver pixmap cache refresh
  516. QDir cacheDir = TomahawkUtils::appDataDir();
  517. if ( cacheDir.cd( "atticacache" ) )
  518. {
  519. QStringList files = cacheDir.entryList( QStringList() << "*.png" );
  520. foreach ( const QString& file, files )
  521. {
  522. const bool removed = cacheDir.remove( file );
  523. tDebug() << "Tried to remove cached image, succeeded?" << removed << cacheDir.filePath( file );
  524. }
  525. }
  526. }
  527. else if ( oldVersion == 13 )
  528. {
  529. //Delete old echonest_stylesandmoods.dat file
  530. QFile dataFile( TomahawkUtils::appDataDir().absoluteFilePath( "echonest_stylesandmoods.dat" ) );
  531. const bool removed = dataFile.remove();
  532. tDebug() << "Tried to remove echonest_stylesandmoods.dat, succeeded?" << removed;
  533. }
  534. else if ( oldVersion == 14 )
  535. {
  536. //No upgrade on OSX: we keep storing credentials in TomahawkSettings
  537. //because QtKeychain and/or OSX Keychain is flaky. --Teo 12/2013
  538. #ifndef Q_OS_MAC
  539. const QStringList accounts = value( "accounts/allaccounts" ).toStringList();
  540. tDebug() << "About to move these accounts to QtKeychain:" << accounts;
  541. //Move storage of Credentials from QSettings to QtKeychain
  542. foreach ( const QString& account, accounts )
  543. {
  544. tDebug() << "beginGroup" << QString( "accounts/%1" ).arg( account );
  545. beginGroup( QString( "accounts/%1" ).arg( account ) );
  546. const QVariantHash hash = value( "credentials" ).toHash();
  547. tDebug() << hash[ "username" ]
  548. << ( hash[ "password" ].isNull() ? ", no password" : ", has password" );
  549. QVariantMap creds;
  550. for ( QVariantHash::const_iterator it = hash.constBegin(); it != hash.constEnd(); ++it )
  551. {
  552. creds.insert( it.key(), it.value() );
  553. }
  554. if ( !creds.isEmpty() )
  555. {
  556. QKeychain::WritePasswordJob* j = new QKeychain::WritePasswordJob( QLatin1String( "Tomahawk" ), this );
  557. j->setKey( account );
  558. j->setAutoDelete( true );
  559. #if defined( Q_OS_UNIX ) && !defined( Q_OS_MAC )
  560. j->setInsecureFallback( true );
  561. #endif
  562. bool ok;
  563. QByteArray data = TomahawkUtils::toJson( creds, &ok );
  564. if ( ok )
  565. {
  566. tDebug() << "Performing upgrade for account" << account;
  567. }
  568. else
  569. {
  570. tDebug() << "Upgrade error: cannot serialize credentials to JSON for account" << account;
  571. }
  572. j->setTextData( data );
  573. j->start();
  574. }
  575. remove( "credentials" );
  576. endGroup();
  577. }
  578. #endif //Q_OS_MAC
  579. }
  580. else if ( oldVersion == 15 )
  581. {
  582. // 0.8.0 switches to Lucene++. Force a reindex.
  583. // (obsoleted by version 16 update)
  584. }
  585. else if ( oldVersion == 16 )
  586. {
  587. Tomahawk::DatabaseFuzzyIndex::wipeIndex();
  588. updateIndex();
  589. }
  590. }
  591. void
  592. TomahawkSettings::setAcceptedLegalWarning( bool accept )
  593. {
  594. setValue( "acceptedLegalWarning", accept );
  595. }
  596. bool
  597. TomahawkSettings::acceptedLegalWarning() const
  598. {
  599. return value( "acceptedLegalWarning", false ).toBool();
  600. }
  601. void
  602. TomahawkSettings::setInfoSystemCacheVersion( uint version )
  603. {
  604. setValue( "infosystemcacheversion", version );
  605. }
  606. uint
  607. TomahawkSettings::infoSystemCacheVersion() const
  608. {
  609. return value( "infosystemcacheversion", 0 ).toUInt();
  610. }
  611. void
  612. TomahawkSettings::setGenericCacheVersion( uint version )
  613. {
  614. setValue( "genericcacheversion", version );
  615. }
  616. uint
  617. TomahawkSettings::genericCacheVersion() const
  618. {
  619. return value( "genericcacheversion", 0 ).toUInt();
  620. }
  621. QString
  622. TomahawkSettings::storageCacheLocation() const
  623. {
  624. return QStandardPaths::writableLocation( QStandardPaths::CacheLocation );
  625. }
  626. QStringList
  627. TomahawkSettings::scannerPaths() const
  628. {
  629. QString musicLocation;
  630. musicLocation = QStandardPaths::writableLocation( QStandardPaths::MusicLocation );
  631. return value( "scanner/paths", musicLocation ).toStringList();
  632. }
  633. void
  634. TomahawkSettings::setScannerPaths( const QStringList& paths )
  635. {
  636. setValue( "scanner/paths", paths );
  637. }
  638. bool
  639. TomahawkSettings::hasScannerPaths() const
  640. {
  641. //FIXME: After enough time, remove this hack
  642. return contains( "scanner/paths" ) || contains( "scannerpath" ) || contains( "scannerpaths" );
  643. }
  644. uint
  645. TomahawkSettings::scannerTime() const
  646. {
  647. return value( "scanner/intervaltime", 60 ).toUInt();
  648. }
  649. void
  650. TomahawkSettings::setScannerTime( uint time )
  651. {
  652. setValue( "scanner/intervaltime", time );
  653. }
  654. bool
  655. TomahawkSettings::watchForChanges() const
  656. {
  657. return value( "scanner/watchforchanges", false ).toBool();
  658. }
  659. void
  660. TomahawkSettings::setWatchForChanges( bool watch )
  661. {
  662. setValue( "scanner/watchforchanges", watch );
  663. }
  664. QString
  665. TomahawkSettings::downloadsPreferredFormat() const
  666. {
  667. return value( "downloadmanager/preferredFormat", "MP3" ).toString();
  668. }
  669. void
  670. TomahawkSettings::setDownloadsPreferredFormat( const QString& format )
  671. {
  672. setValue( "downloadmanager/preferredFormat", format );
  673. }
  674. QString
  675. TomahawkSettings::downloadsPath() const
  676. {
  677. QString musicLocation;
  678. if ( !scannerPaths().isEmpty() )
  679. musicLocation = scannerPaths().first();
  680. return value( "downloadmanager/path", musicLocation ).toString();
  681. }
  682. void
  683. TomahawkSettings::setDownloadsPath( const QString& path )
  684. {
  685. setValue( "downloadmanager/path", path );
  686. }
  687. QVariantList
  688. TomahawkSettings::downloadStates() const
  689. {
  690. return value( "downloadmanager/states", QVariantList() ).toList();
  691. }
  692. void
  693. TomahawkSettings::setDownloadStates( const QVariantList& downloads )
  694. {
  695. setValue( "downloadmanager/states", downloads );
  696. }
  697. bool
  698. TomahawkSettings::httpEnabled() const
  699. {
  700. return value( "network/http", true ).toBool();
  701. }
  702. void
  703. TomahawkSettings::setHttpEnabled( bool enable )
  704. {
  705. setValue( "network/http", enable );
  706. }
  707. bool
  708. TomahawkSettings::httpBindAll() const
  709. {
  710. return value ( "network/httpbindall", false ).toBool();
  711. }
  712. void
  713. TomahawkSettings::setHttpBindAll( bool bindAll )
  714. {
  715. setValue( "network/httpbindall", bindAll );
  716. }
  717. bool
  718. TomahawkSettings::crashReporterEnabled() const
  719. {
  720. return value( "ui/crashReporter", true ).toBool();
  721. }
  722. void
  723. TomahawkSettings::setCrashReporterEnabled( bool enable )
  724. {
  725. setValue( "ui/crashReporter", enable );
  726. }
  727. bool
  728. TomahawkSettings::songChangeNotificationEnabled() const
  729. {
  730. return value( "ui/songChangeNotification", true ).toBool();
  731. }
  732. void
  733. TomahawkSettings::setSongChangeNotificationEnabled(bool enable)
  734. {
  735. setValue( "ui/songChangeNotification", enable );
  736. }
  737. bool
  738. TomahawkSettings::autoDetectExternalIp() const
  739. {
  740. return value( "network/auto-detect-external-ip" ).toBool();
  741. }
  742. void
  743. TomahawkSettings::setAutoDetectExternalIp( bool autoDetect )
  744. {
  745. setValue( "network/auto-detect-external-ip", autoDetect );
  746. }
  747. unsigned int
  748. TomahawkSettings::volume() const
  749. {
  750. return value( "audio/volume", 75 ).toUInt();
  751. }
  752. void
  753. TomahawkSettings::setVolume( unsigned int volume )
  754. {
  755. setValue( "audio/volume", volume );
  756. }
  757. bool
  758. TomahawkSettings::muted() const
  759. {
  760. return value( "audio/muted" ).toBool();
  761. }
  762. void
  763. TomahawkSettings::setMuted( bool muted )
  764. {
  765. setValue( "audio/muted", muted );
  766. }
  767. QString
  768. TomahawkSettings::proxyHost() const
  769. {
  770. return value( "network/proxy/host", QString() ).toString();
  771. }
  772. void
  773. TomahawkSettings::setProxyHost( const QString& host )
  774. {
  775. setValue( "network/proxy/host", host );
  776. }
  777. QString
  778. TomahawkSettings::proxyNoProxyHosts() const
  779. {
  780. return value( "network/proxy/noproxyhosts", QString() ).toString();
  781. }
  782. void
  783. TomahawkSettings::setProxyNoProxyHosts( const QString& hosts )
  784. {
  785. setValue( "network/proxy/noproxyhosts", hosts );
  786. }
  787. qulonglong
  788. TomahawkSettings::proxyPort() const
  789. {
  790. return value( "network/proxy/port", 1080 ).toULongLong();
  791. }
  792. void
  793. TomahawkSettings::setProxyPort( const qulonglong port )
  794. {
  795. setValue( "network/proxy/port", port );
  796. }
  797. QString
  798. TomahawkSettings::proxyUsername() const
  799. {
  800. return value( "network/proxy/username", QString() ).toString();
  801. }
  802. void
  803. TomahawkSettings::setProxyUsername( const QString& username )
  804. {
  805. setValue( "network/proxy/username", username );
  806. }
  807. QString
  808. TomahawkSettings::proxyPassword() const
  809. {
  810. return value( "network/proxy/password", QString() ).toString();
  811. }
  812. void
  813. TomahawkSettings::setProxyPassword( const QString& password )
  814. {
  815. setValue( "network/proxy/password", password );
  816. }
  817. QNetworkProxy::ProxyType
  818. TomahawkSettings::proxyType() const
  819. {
  820. return static_cast< QNetworkProxy::ProxyType>( value( "network/proxy/type", QNetworkProxy::NoProxy ).toInt() );
  821. }
  822. void
  823. TomahawkSettings::setProxyType( const QNetworkProxy::ProxyType type )
  824. {
  825. setValue( "network/proxy/type", static_cast< uint >( type ) );
  826. }
  827. bool
  828. TomahawkSettings::proxyDns() const
  829. {
  830. return value( "network/proxy/dns", false ).toBool();
  831. }
  832. void
  833. TomahawkSettings::setProxyDns( bool lookupViaProxy )
  834. {
  835. setValue( "network/proxy/dns", lookupViaProxy );
  836. }
  837. QVariantList
  838. TomahawkSettings::aclEntries() const
  839. {
  840. QVariant retVal = value( "acl/entries", QVariantList() );
  841. if ( retVal.isValid() && retVal.canConvert< QVariantList >() )
  842. return retVal.toList();
  843. return QVariantList();
  844. }
  845. void
  846. TomahawkSettings::setAclEntries( const QVariantList &entries )
  847. {
  848. tDebug() << "Setting entries";
  849. setValue( "acl/entries", entries );
  850. sync();
  851. tDebug() << "Done setting entries";
  852. }
  853. bool
  854. TomahawkSettings::isSslCertKnown( const QByteArray& sslDigest ) const
  855. {
  856. return value( "network/ssl/certs" ).toMap().contains( sslDigest );
  857. }
  858. bool
  859. TomahawkSettings::isSslCertTrusted( const QByteArray& sslDigest ) const
  860. {
  861. return value( "network/ssl/certs" ).toMap().value( sslDigest, false ).toBool();
  862. }
  863. void
  864. TomahawkSettings::setSslCertTrusted( const QByteArray& sslDigest, bool trusted )
  865. {
  866. QVariantMap map = value( "network/ssl/certs" ).toMap();
  867. map[ sslDigest ] = trusted;
  868. setValue( "network/ssl/certs", map );
  869. }
  870. QByteArray
  871. TomahawkSettings::mainWindowGeometry() const
  872. {
  873. return value( "ui/mainwindow/geometry" ).toByteArray();
  874. }
  875. void
  876. TomahawkSettings::setMainWindowGeometry( const QByteArray& geom )
  877. {
  878. setValue( "ui/mainwindow/geometry", geom );
  879. }
  880. QByteArray
  881. TomahawkSettings::mainWindowState() const
  882. {
  883. return value( "ui/mainwindow/state" ).toByteArray();
  884. }
  885. void
  886. TomahawkSettings::setMainWindowState( const QByteArray& state )
  887. {
  888. setValue( "ui/mainwindow/state", state );
  889. }
  890. QByteArray
  891. TomahawkSettings::mainWindowSplitterState() const
  892. {
  893. return value( "ui/mainwindow/splitterState" ).toByteArray();
  894. }
  895. void
  896. TomahawkSettings::setMainWindowSplitterState( const QByteArray& state )
  897. {
  898. setValue( "ui/mainwindow/splitterState", state );
  899. }
  900. bool
  901. TomahawkSettings::verboseNotifications() const
  902. {
  903. return value( "ui/notifications/verbose", false ).toBool();
  904. }
  905. void
  906. TomahawkSettings::setVerboseNotifications( bool notifications )
  907. {
  908. setValue( "ui/notifications/verbose", notifications );
  909. }
  910. bool
  911. TomahawkSettings::menuBarVisible() const
  912. {
  913. #ifndef Q_OS_MAC
  914. return value( "ui/mainwindow/menuBarVisible", true ).toBool();
  915. #else
  916. return true;
  917. #endif
  918. }
  919. void
  920. TomahawkSettings::setMenuBarVisible( bool visible )
  921. {
  922. #ifndef Q_OS_MAC
  923. setValue( "ui/mainwindow/menuBarVisible", visible );
  924. #endif
  925. }
  926. bool
  927. TomahawkSettings::fullscreenEnabled() const
  928. {
  929. return value( "ui/mainwindow/fullscreenEnabled", false ).toBool();
  930. }
  931. void
  932. TomahawkSettings::setFullscreenEnabled( bool enabled )
  933. {
  934. setValue( "ui/mainwindow/fullscreenEnabled", enabled );
  935. }
  936. bool
  937. TomahawkSettings::showOfflineSources() const
  938. {
  939. return value( "collection/sources/showoffline", false ).toBool();
  940. }
  941. void
  942. TomahawkSettings::setShowOfflineSources( bool show )
  943. {
  944. setValue( "collection/sources/showoffline", show );
  945. }
  946. bool
  947. TomahawkSettings::enableEchonestCatalogs() const
  948. {
  949. return value( "collection/enable_catalogs", false ).toBool();
  950. }
  951. void
  952. TomahawkSettings::setEnableEchonestCatalogs( bool enable )
  953. {
  954. setValue( "collection/enable_catalogs", enable );
  955. }
  956. QByteArray
  957. TomahawkSettings::playlistColumnSizes( const QString& playlistid ) const
  958. {
  959. return value( QString( "ui/playlist/%1/columnSizes" ).arg( playlistid ) ).toByteArray();
  960. }
  961. void
  962. TomahawkSettings::setPlaylistColumnSizes( const QString& playlistid, const QByteArray& state )
  963. {
  964. if ( playlistid.isEmpty() )
  965. return;
  966. setValue( QString( "ui/playlist/%1/columnSizes" ).arg( playlistid ), state );
  967. }
  968. bool
  969. TomahawkSettings::shuffleState( const QString& playlistid ) const
  970. {
  971. return value( QString( "ui/playlist/%1/shuffleState" ).arg( playlistid ) ).toBool();
  972. }
  973. void
  974. TomahawkSettings::setShuffleState( const QString& playlistid, bool state)
  975. {
  976. setValue( QString( "ui/playlist/%1/shuffleState" ).arg( playlistid ), state );
  977. }
  978. void
  979. TomahawkSettings::removePlaylistSettings( const QString& playlistid )
  980. {
  981. remove( QString( "ui/playlist/%1/shuffleState" ).arg( playlistid ) );
  982. remove( QString( "ui/playlist/%1/repeatMode" ).arg( playlistid ) );
  983. }
  984. QVariant
  985. TomahawkSettings::queueState() const
  986. {
  987. return value( QString( "playlists/queue/state" ) );
  988. }
  989. void
  990. TomahawkSettings::setQueueState( const QVariant& state )
  991. {
  992. setValue( QString( "playlists/queue/state" ), state );
  993. }
  994. void
  995. TomahawkSettings::setRepeatMode( const QString& playlistid, Tomahawk::PlaylistModes::RepeatMode mode )
  996. {
  997. setValue( QString( "ui/playlist/%1/repeatMode" ).arg( playlistid ), (int)mode );
  998. }
  999. Tomahawk::PlaylistModes::RepeatMode
  1000. TomahawkSettings::repeatMode( const QString& playlistid )
  1001. {
  1002. return (PlaylistModes::RepeatMode)value( QString( "ui/playlist/%1/repeatMode" ).arg( playlistid ) ).toInt();
  1003. }
  1004. QStringList
  1005. TomahawkSettings::recentlyPlayedPlaylistGuids( unsigned int amount ) const
  1006. {
  1007. QStringList p = value( "playlists/recentlyPlayed" ).toStringList();
  1008. while ( amount && p.count() > (int)amount )
  1009. p.removeAt( 0 );
  1010. return p;
  1011. }
  1012. void
  1013. TomahawkSettings::appendRecentlyPlayedPlaylist( const QString& playlistguid, int sourceId )
  1014. {
  1015. QStringList playlist_guids = value( "playlists/recentlyPlayed" ).toStringList();
  1016. playlist_guids.removeAll( playlistguid );
  1017. playlist_guids.append( playlistguid );
  1018. setValue( "playlists/recentlyPlayed", playlist_guids );
  1019. emit recentlyPlayedPlaylistAdded( playlistguid, sourceId );
  1020. }
  1021. QString
  1022. TomahawkSettings::bookmarkPlaylist() const
  1023. {
  1024. return value( "playlists/bookmark", QString() ).toString();
  1025. }
  1026. void
  1027. TomahawkSettings::setBookmarkPlaylist( const QString& guid )
  1028. {
  1029. setValue( "playlists/bookmark", guid );
  1030. }
  1031. QStringList
  1032. TomahawkSettings::sipPlugins() const
  1033. {
  1034. return value( "sip/allplugins", QStringList() ).toStringList();
  1035. }
  1036. void
  1037. TomahawkSettings::setSipPlugins( const QStringList& plugins )
  1038. {
  1039. setValue( "sip/allplugins", plugins );
  1040. }
  1041. QStringList
  1042. TomahawkSettings::enabledSipPlugins() const
  1043. {
  1044. return value( "sip/enabledplugins", QStringList() ).toStringList();
  1045. }
  1046. void
  1047. TomahawkSettings::setEnabledSipPlugins( const QStringList& list )
  1048. {
  1049. setValue( "sip/enabledplugins", list );
  1050. }
  1051. void
  1052. TomahawkSettings::enableSipPlugin( const QString& pluginId )
  1053. {
  1054. QStringList list = enabledSipPlugins();
  1055. list << pluginId;
  1056. setEnabledSipPlugins( list );
  1057. }
  1058. void
  1059. TomahawkSettings::disableSipPlugin( const QString& pluginId )
  1060. {
  1061. QStringList list = enabledSipPlugins();
  1062. list.removeAll( pluginId );
  1063. setEnabledSipPlugins( list );
  1064. }
  1065. void
  1066. TomahawkSettings::addSipPlugin( const QString& pluginId, bool enable )
  1067. {
  1068. QStringList list = sipPlugins();
  1069. list << pluginId;
  1070. setSipPlugins( list );
  1071. if ( enable )
  1072. enableSipPlugin( pluginId );
  1073. }
  1074. void
  1075. TomahawkSettings::removeSipPlugin( const QString& pluginId )
  1076. {
  1077. QStringList list = sipPlugins();
  1078. list.removeAll( pluginId );
  1079. setSipPlugins( list );
  1080. if( enabledSipPlugins().contains( pluginId ) )
  1081. disableSipPlugin( pluginId );
  1082. }
  1083. QStringList
  1084. TomahawkSettings::accounts() const
  1085. {
  1086. QStringList accounts = value( "accounts/allaccounts", QStringList() ).toStringList();
  1087. accounts.removeDuplicates();
  1088. return accounts;
  1089. }
  1090. void
  1091. TomahawkSettings::setAccounts( const QStringList& accountIds )
  1092. {
  1093. QStringList accounts = accountIds;
  1094. accounts.removeDuplicates();
  1095. setValue( "accounts/allaccounts", accounts );
  1096. }
  1097. void
  1098. TomahawkSettings::addAccount( const QString& accountId )
  1099. {
  1100. QStringList list = accounts();
  1101. list << accountId;
  1102. setAccounts( list );
  1103. }
  1104. void
  1105. TomahawkSettings::removeAccount( const QString& accountId )
  1106. {
  1107. QStringList list = accounts();
  1108. list.removeAll( accountId );
  1109. setAccounts( list );
  1110. }
  1111. Tomahawk::Network::ExternalAddress::Mode
  1112. TomahawkSettings::externalAddressMode()
  1113. {
  1114. if ( value( "network/prefer-static-host-and-port", false ).toBool() )
  1115. {
  1116. remove( "network/prefer-static-host-and-port" );
  1117. setValue( "network/external-address-mode", Tomahawk::Network::ExternalAddress::Static );
  1118. }
  1119. return (Tomahawk::Network::ExternalAddress::Mode) value( "network/external-address-mode", Tomahawk::Network::ExternalAddress::Upnp ).toInt();
  1120. }
  1121. void
  1122. TomahawkSettings::setExternalAddressMode( Tomahawk::Network::ExternalAddress::Mode externalAddressMode )
  1123. {
  1124. setValue( "network/external-address-mode", externalAddressMode );
  1125. }
  1126. QString
  1127. TomahawkSettings::externalHostname() const
  1128. {
  1129. return value( "network/external-hostname" ).toString();
  1130. }
  1131. void
  1132. TomahawkSettings::setExternalHostname(const QString& externalHostname)
  1133. {
  1134. setValue( "network/external-hostname", externalHostname );
  1135. }
  1136. int
  1137. TomahawkSettings::defaultPort() const
  1138. {
  1139. return 50210;
  1140. }
  1141. int
  1142. TomahawkSettings::externalPort() const
  1143. {
  1144. return value( "network/external-port", 50210 ).toInt();
  1145. }
  1146. void
  1147. TomahawkSettings::setExternalPort(int externalPort)
  1148. {
  1149. if ( externalPort == 0 )
  1150. setValue( "network/external-port", 50210);
  1151. else
  1152. setValue( "network/external-port", externalPort);
  1153. }
  1154. QString
  1155. TomahawkSettings::xmppBotServer() const
  1156. {
  1157. return value( "xmppBot/server", QString() ).toString();
  1158. }
  1159. void
  1160. TomahawkSettings::setXmppBotServer( const QString& server )
  1161. {
  1162. setValue( "xmppBot/server", server );
  1163. }
  1164. QString
  1165. TomahawkSettings::xmppBotJid() const
  1166. {
  1167. return value( "xmppBot/jid", QString() ).toString();
  1168. }
  1169. void
  1170. TomahawkSettings::setXmppBotJid( const QString& component )
  1171. {
  1172. setValue( "xmppBot/jid", component );
  1173. }
  1174. QString
  1175. TomahawkSettings::xmppBotPassword() const
  1176. {
  1177. return value( "xmppBot/password", QString() ).toString();
  1178. }
  1179. void
  1180. TomahawkSettings::setXmppBotPassword( const QString& password )
  1181. {
  1182. setValue( "xmppBot/password", password );
  1183. }
  1184. int
  1185. TomahawkSettings::xmppBotPort() const
  1186. {
  1187. return value( "xmppBot/port", -1 ).toInt();
  1188. }
  1189. void
  1190. TomahawkSettings::setXmppBotPort( const int port )
  1191. {
  1192. setValue( "xmppBot/port", port );
  1193. }
  1194. QString
  1195. TomahawkSettings::scriptDefaultPath() const
  1196. {
  1197. return value( "script/defaultpath", QDir::homePath() ).toString();
  1198. }
  1199. void
  1200. TomahawkSettings::setScriptDefaultPath( const QString& path )
  1201. {
  1202. setValue( "script/defaultpath", path );
  1203. }
  1204. QString
  1205. TomahawkSettings::playlistDefaultPath() const
  1206. {
  1207. return value( "playlists/defaultpath", QDir::homePath() ).toString();
  1208. }
  1209. void
  1210. TomahawkSettings::setPlaylistDefaultPath( const QString& path )
  1211. {
  1212. setValue( "playlists/defaultpath", path );
  1213. }
  1214. bool
  1215. TomahawkSettings::nowPlayingEnabled() const
  1216. {
  1217. return value( "adium/enablenowplaying", false ).toBool();
  1218. }
  1219. void
  1220. TomahawkSettings::setNowPlayingEnabled( bool enable )
  1221. {
  1222. setValue( "adium/enablenowplaying", enable );
  1223. }
  1224. TomahawkSettings::PrivateListeningMode
  1225. TomahawkSettings::privateListeningMode() const
  1226. {
  1227. return ( TomahawkSettings::PrivateListeningMode ) value( "privatelisteningmode", TomahawkSettings::PublicListening ).toInt();
  1228. }
  1229. void
  1230. TomahawkSettings::setPrivateListeningMode( TomahawkSettings::PrivateListeningMode mode )
  1231. {
  1232. setValue( "privatelisteningmode", mode );
  1233. }
  1234. void
  1235. TomahawkSettings::updateIndex()
  1236. {
  1237. if ( !Database::instance() || !Database::instance()->isReady() )
  1238. {
  1239. QTimer::singleShot( 0, this, SLOT( updateIndex() ) );
  1240. return;
  1241. }
  1242. tDebug() << Q_FUNC_INFO << "Updating fuzzy index.";
  1243. Tomahawk::DatabaseCommand* cmd = new Tomahawk::DatabaseCommand_UpdateSearchIndex();
  1244. Database::instance()->enqueue( QSharedPointer<Tomahawk::DatabaseCommand>( cmd ) );
  1245. }
  1246. QString
  1247. TomahawkSettings::importPlaylistPath() const
  1248. {
  1249. if ( contains( "importPlaylistPath" ) )
  1250. return value( "importPlaylistPath" ).toString();
  1251. else
  1252. return QDir::homePath();
  1253. }
  1254. void
  1255. TomahawkSettings::setImportPlaylistPath( const QString& path )
  1256. {
  1257. setValue( "importPlaylistPath", path );
  1258. }
  1259. SerializedUpdaters
  1260. TomahawkSettings::playlistUpdaters() const
  1261. {
  1262. return value( "playlists/updaters" ).value< SerializedUpdaters >();
  1263. }
  1264. void
  1265. TomahawkSettings::setPlaylistUpdaters( const SerializedUpdaters& updaters )
  1266. {
  1267. setValue( "playlists/updaters", QVariant::fromValue< SerializedUpdaters >( updaters ) );
  1268. }
  1269. void
  1270. TomahawkSettings::setLastChartIds( const QMap<QString, QVariant>& ids ){
  1271. setValue( "chartIds", QVariant::fromValue<QMap<QString, QVariant> >( ids ) );
  1272. }
  1273. QMap<QString, QVariant> TomahawkSettings::lastChartIds(){
  1274. return value( "chartIds" ).value<QMap<QString, QVariant> >();
  1275. }
  1276. inline QDataStream&
  1277. operator<<( QDataStream& out, const AtticaManager::StateHash& states )
  1278. {
  1279. out << TOMAHAWK_SETTINGS_VERSION;
  1280. out << (quint32)states.count();
  1281. foreach( const QString& key, states.keys() )
  1282. {
  1283. AtticaManager::Resolver resolver = states[ key ];
  1284. out << key << resolver.version << resolver.scriptPath << (qint32)resolver.state << resolver.userRating << resolver.binary;
  1285. }
  1286. return out;
  1287. }
  1288. inline QDataStream&
  1289. operator>>( QDataStream& in, AtticaManager::StateHash& states )
  1290. {
  1291. quint32 count = 0, configVersion = 0;
  1292. in >> configVersion;
  1293. in >> count;
  1294. for ( uint i = 0; i < count; i++ )
  1295. {
  1296. QString key, version, scriptPath;
  1297. qint32 state, userRating;
  1298. bool binary = false;
  1299. in >> key;
  1300. in >> version;
  1301. in >> scriptPath;
  1302. in >> state;
  1303. in >> userRating;
  1304. if ( configVersion > 10 )
  1305. {
  1306. // V11 includes 'bool binary' flag
  1307. in >> binary;
  1308. }
  1309. states[ key ] = AtticaManager::Resolver( version, scriptPath, userRating, (AtticaManager::ResolverState)state, binary );
  1310. }
  1311. return in;
  1312. }
  1313. void
  1314. TomahawkSettings::registerCustomSettingsHandlers()
  1315. {
  1316. qRegisterMetaType< Tomahawk::SerializedUpdater >( "Tomahawk::SerializedUpdater" );
  1317. qRegisterMetaType< Tomahawk::SerializedUpdaters >( "Tomahawk::SerializedUpdaters" );
  1318. qRegisterMetaTypeStreamOperators< Tomahawk::SerializedUpdaters >( "Tomahawk::SerializedUpdaters" );
  1319. qRegisterMetaType< AtticaManager::StateHash >( "AtticaManager::StateHash" );
  1320. qRegisterMetaTypeStreamOperators< AtticaManager::StateHash >( "AtticaManager::StateHash" );
  1321. }
  1322. void
  1323. TomahawkSettings::setAtticaResolverState( const QString& resolver, AtticaManager::ResolverState state )
  1324. {
  1325. AtticaManager::StateHash resolvers = value( "script/atticaresolverstates" ).value< AtticaManager::StateHash >();
  1326. AtticaManager::Resolver r = resolvers.value( resolver );
  1327. r.state = state;
  1328. resolvers.insert( resolver, r );
  1329. setValue( "script/atticaresolverstates", QVariant::fromValue< AtticaManager::StateHash >( resolvers ) );
  1330. sync();
  1331. }
  1332. AtticaManager::StateHash
  1333. TomahawkSettings::atticaResolverStates() const
  1334. {
  1335. return value( "script/atticaresolverstates" ).value< AtticaManager::StateHash >();
  1336. }
  1337. void
  1338. TomahawkSettings::setAtticaResolverStates( const AtticaManager::StateHash states )
  1339. {
  1340. setValue( "script/atticaresolverstates", QVariant::fromValue< AtticaManager::StateHash >( states ) );
  1341. }
  1342. void
  1343. TomahawkSettings::removeAtticaResolverState ( const QString& resolver )
  1344. {
  1345. AtticaManager::StateHash resolvers = value( "script/atticaresolverstates" ).value< AtticaManager::StateHash >();
  1346. resolvers.remove( resolver );
  1347. setValue( "script/atticaresolverstates", QVariant::fromValue< AtticaManager::StateHash >( resolvers ) );
  1348. }
  1349. QByteArray
  1350. TomahawkSettings::playdarCertificate() const
  1351. {
  1352. return value( "playdar/certificate").value< QByteArray >();
  1353. }
  1354. void
  1355. TomahawkSettings::setPlaydarCertificate( const QByteArray& cert )
  1356. {
  1357. setValue( "playdar/certificate", cert );
  1358. }
  1359. QByteArray
  1360. TomahawkSettings::playdarKey() const
  1361. {
  1362. return value( "playdar/key" ).value< QByteArray >();
  1363. }
  1364. void
  1365. TomahawkSettings::setPlaydarKey( const QByteArray& key )
  1366. {
  1367. setValue( "playdar/key", key );
  1368. }
  1369. QString
  1370. TomahawkSettings::vlcArguments() const
  1371. {
  1372. return value( "vlc/cmdline_args" ).value< QString >();
  1373. }
  1374. void
  1375. TomahawkSettings::setVlcArguments( const QString& args )
  1376. {
  1377. setValue( "vlc/cmdline_args", args);
  1378. }