File indexing completed on 2024-05-19 04:50:16

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Shane King <kde@dontletsstart.com>                                *
0003  * Copyright (c) 2009 Leo Franchi <lfranchi@kde.org>                                    *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #ifndef LASTFMSERVICECONFIG_H
0019 #define LASTFMSERVICECONFIG_H
0020 
0021 #include "services/lastfm/amarok_service_lastfm_config_export.h"
0022 
0023 #include <QObject>
0024 #include <QSharedPointer>
0025 #include <QString>
0026 
0027 namespace KWallet {
0028     class Wallet;
0029 }
0030 class QMessageBox;
0031 class LastFmServiceConfig;
0032 typedef QSharedPointer<LastFmServiceConfig> LastFmServiceConfigPtr;
0033 
0034 /**
0035  * Configuration of the Last.fm plugin. Because some operations are async, you should
0036  * connect to the updated() signal and listen to changes, especially ones to username,
0037  * password or sessionKey.
0038  */
0039 class AMAROK_SERVICE_LASTFM_CONFIG_EXPORT LastFmServiceConfig : public QObject
0040 {
0041     Q_OBJECT
0042 public:
0043     static const char *configSectionName() { return "Service_LastFm"; }
0044 
0045     /**
0046      * Singleton pattern accessor. Not thread safe - must be called from the main
0047      * thread.
0048      */
0049     static LastFmServiceConfigPtr instance();
0050 
0051     ~LastFmServiceConfig() override;
0052 
0053     /**
0054      * Saves the configuration back to the storage and notifies other users about
0055      * the change. This method must be called after calling any of the set* methods.
0056      */
0057     void save();
0058 
0059     QString username() const { return m_username; }
0060     void setUsername( const QString &username ) { m_username = username; }
0061 
0062     QString password() const { return m_password; }
0063     void setPassword( const QString &password ) { m_password = password; }
0064 
0065     QString sessionKey() const { return m_sessionKey; }
0066     void setSessionKey( const QString &sessionKey ) { m_sessionKey = sessionKey; }
0067 
0068     bool scrobble() const { return m_scrobble; }
0069     static bool defaultScrobble() { return true; }
0070     void setScrobble( bool scrobble ) { m_scrobble = scrobble; }
0071 
0072     bool fetchSimilar() const { return m_fetchSimilar; }
0073     static bool defaultFetchSimilar() { return true; }
0074     void setFetchSimilar( bool fetchSimilar ) { m_fetchSimilar = fetchSimilar; }
0075 
0076     bool scrobbleComposer() const { return m_scrobbleComposer; }
0077     static bool defaultScrobbleComposer() { return false; }
0078     void setScrobbleComposer( bool scrobbleComposer ) { m_scrobbleComposer = scrobbleComposer; }
0079 
0080     bool useFancyRatingTags() const { return m_useFancyRatingTags; }
0081     static bool defaultUseFancyRatingTags() { return true; }
0082     void setUseFancyRatingTags( bool useFancyRatingTags ) { m_useFancyRatingTags = useFancyRatingTags; }
0083 
0084     bool announceCorrections() const { return m_announceCorrections; }
0085     static bool defaultAnnounceCorrections() { return true; }
0086     void setAnnounceCorrections( bool announceCorrections ) { m_announceCorrections = announceCorrections; }
0087 
0088     bool filterByLabel() const { return m_filterByLabel; }
0089     static bool defaultFilterByLabel() { return false; }
0090     void setFilterByLabel( bool filterByLabel ) { m_filterByLabel = filterByLabel; }
0091 
0092     QString filteredLabel() const { return m_filteredLabel; }
0093     static QString defaultFilteredLabel() { return QString(); }
0094     void setFilteredLabel( const QString &filteredLabel ) { m_filteredLabel = filteredLabel; }
0095 
0096 Q_SIGNALS:
0097     /**
0098      * Emitted when settings are changed. (after save() is called)
0099      */
0100     void updated();
0101 
0102 private Q_SLOTS:
0103     void slotWalletOpenedToRead( bool success );
0104     void slotWalletOpenedToWrite( bool success );
0105 
0106     void slotStoreCredentialsInAscii();
0107 
0108 private:
0109     Q_DISABLE_COPY( LastFmServiceConfig )
0110     LastFmServiceConfig();
0111 
0112     void openWalletToRead();
0113     void openWalletToWrite();
0114     void openWalletAsync();
0115     void prepareOpenedWallet();
0116     void askAboutMissingKWallet();
0117 
0118     // don't remove or reorder entries, would break saved config
0119     enum KWalletUsage {
0120         NoPasswordEnteredYet,
0121         PasswodInKWallet,
0122         PasswordInAscii
0123     };
0124 
0125     QString m_username;
0126     QString m_password;
0127     QString m_sessionKey;
0128     bool m_scrobble;
0129     bool m_fetchSimilar;
0130     bool m_scrobbleComposer;
0131     bool m_useFancyRatingTags;
0132     bool m_announceCorrections;
0133     bool m_filterByLabel;
0134     QString m_filteredLabel;
0135     KWalletUsage m_kWalletUsage;
0136 
0137     QMessageBox* m_askDiag;
0138     KWallet::Wallet* m_wallet;
0139 
0140     static QWeakPointer<LastFmServiceConfig> s_instance;
0141 };
0142 
0143 #endif // LASTFMSERVICECONFIG_H