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

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Casey Link <unnamedrambler@gmail.com>                             *
0003  * Copyright (c) 2009 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0004  * Copyright (c) 2009 Mark Kretschmann <kretschmann@kde.org>                            *
0005  *                                                                                      *
0006  * This program is free software; you can redistribute it and/or modify it under        *
0007  * the terms of the GNU General Public License as published by the Free Software        *
0008  * Foundation; either version 2 of the License, or (at your option) any later           *
0009  * version.                                                                             *
0010  *                                                                                      *
0011  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0013  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0014  *                                                                                      *
0015  * You should have received a copy of the GNU General Public License along with         *
0016  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0017  ****************************************************************************************/
0018 
0019 #ifndef LASTFMTREEMODEL_H
0020 #define LASTFMTREEMODEL_H
0021 
0022 #include "core/meta/forward_declarations.h"
0023 
0024 #include <QUrl>
0025 
0026 #include <QAbstractItemModel>
0027 #include <QPixmap>
0028 
0029 #include <User.h>
0030 
0031 
0032 namespace LastFm
0033 {
0034 enum Type
0035 {
0036     Root = 0,
0037     MyRecommendations,
0038     PersonalRadio,
0039     MixRadio,
0040     TopArtists,
0041     MyTags,
0042     Friends,
0043     RowCount,
0044     MyTagsChild,
0045     FriendsChild,
0046     ArtistsChild,
0047     RecentlyBannedTrack,
0048     RecentlyPlayedTrack,
0049     RecentlyLovedTrack,
0050     HistoryStation,
0051     UserChildPersonal,
0052     TypeUnknown
0053 };
0054 
0055 enum Role
0056 {
0057     StationUrlRole = Qt::UserRole,
0058     UrlRole,
0059     TrackRole,
0060     TypeRole
0061 };
0062 
0063 enum SortOrder
0064 {
0065     MostWeightOrder,
0066     AscendingOrder,
0067     DescendingOrder
0068 };
0069 
0070 
0071 }
0072 
0073 class LastFmTreeItem;
0074 class QUrl;
0075 
0076 
0077 class LastFmTreeModel : public QAbstractItemModel
0078 {
0079     Q_OBJECT
0080 
0081 public:
0082     explicit LastFmTreeModel( QObject *parent = nullptr );
0083     ~LastFmTreeModel() override;
0084 
0085     QVariant data( const QModelIndex &index, int role ) const override;
0086     Qt::ItemFlags flags( const QModelIndex &index ) const override;
0087     QModelIndex index( int row, int column,
0088                         const QModelIndex &parent = QModelIndex() ) const override;
0089     QModelIndex parent( const QModelIndex &index ) const override;
0090     int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
0091     int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
0092     static int avatarSize();
0093     void prepareAvatar( QPixmap& avatar, int size );
0094 
0095     QMimeData *mimeData( const QModelIndexList &indices ) const override;
0096 
0097 private Q_SLOTS:
0098     void onAvatarDownloaded( const QString& username, QPixmap );
0099     void slotAddFriends();
0100     void slotAddTags();
0101     void slotAddTopArtists();
0102 
0103 private:
0104     void setupModelData( LastFmTreeItem *parent );
0105 
0106     QIcon avatar( const QString &username, const QUrl &avatarUrl ) const;
0107     QString mapTypeToUrl( LastFm::Type type, const QString &key = "" );
0108 
0109     void appendUserStations( LastFmTreeItem* item, const QString& user );
0110 
0111     lastfm::User m_user;
0112 
0113     LastFmTreeItem *m_rootItem;
0114     LastFmTreeItem *m_myTags;
0115     LastFmTreeItem *m_myFriends;
0116     LastFmTreeItem *m_myTopArtists;
0117     QHash<QString, QIcon> m_avatars;
0118 };
0119 
0120 class LastFmTreeItem
0121 {
0122 public:
0123     LastFmTreeItem ( const LastFm::Type &type, const QVariant &data, LastFmTreeItem *parent = nullptr );
0124     LastFmTreeItem ( const QString &url, const LastFm::Type &type, const QVariant &data, LastFmTreeItem *parent = nullptr );
0125     explicit LastFmTreeItem ( const LastFm::Type &type, LastFmTreeItem *parent = nullptr );
0126     LastFmTreeItem ( const QString &url, const LastFm::Type &type, LastFmTreeItem *parent = nullptr );
0127     ~LastFmTreeItem();
0128 
0129     void appendChild ( LastFmTreeItem *child );
0130 
0131     LastFmTreeItem *child ( int row );
0132     int childCount() const;
0133     QVariant data () const;
0134     int row() const;
0135     LastFmTreeItem *parent();
0136     Meta::TrackPtr track() const;
0137     LastFm::Type type() const
0138     {
0139         return mType;
0140     }
0141 
0142     QUrl avatarUrl() const
0143     {
0144         return avatar;
0145     }
0146     void setAvatarUrl( const QUrl &url )
0147     {
0148         avatar = url;
0149     }
0150 
0151 private:
0152     QList<LastFmTreeItem*> childItems;
0153     LastFm::Type mType;
0154     LastFmTreeItem *parentItem;
0155     QVariant itemData;
0156     QString mUrl;
0157     QUrl avatar;
0158 };
0159 
0160 #endif