File indexing completed on 2024-05-19 04:49:49

0001 /****************************************************************************************
0002  * Copyright (c) 2008-2009 Nikolaj Hald Nielsen <nhn@kde.org>                           *
0003  * Copyright (c) 2009 Seb Ruiz <ruiz@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 PLAYLISTLAYOUTMANAGER_H
0019 #define PLAYLISTLAYOUTMANAGER_H
0020 
0021 #include "LayoutItemConfig.h"
0022 
0023 #include <QStringList>
0024 #include <QString>
0025 #include <QMap>
0026 
0027 class QDomElement;
0028 class QDomDocument;
0029 
0030 namespace Playlist {
0031 
0032 /**
0033  * Class for keeping track of playlist layouts and loading/saving them to xml files. Also keeps track
0034  * Of the order in which these layouts are shown to the user and makes this persistent between sessions.
0035  * @author Nikolaj Hald Nielsen <nhn@kde.org>
0036  */
0037 class LayoutManager : public QObject
0038 {
0039     Q_OBJECT
0040 
0041 public:
0042     /**
0043      * Get the class instance (Singleton Pattern).
0044      * @return The class instance.
0045      */
0046     static LayoutManager* instance();
0047 
0048     /**
0049      * Get the ordered list of layout names.
0050      * @return The list of layout names.
0051      */
0052     QStringList layouts() const;
0053     
0054     /**
0055      * Set the layout that is to be made active and used in the playlist.
0056      * @param layout The name of the layout.
0057      */
0058     void setActiveLayout( const QString &layout );
0059     
0060     /**
0061      * Preview a layout in the playlist. This layout does not need to be stored or have a name yet.
0062      * @param layout The layout to preview.
0063      */
0064     void setPreviewLayout( const PlaylistLayout &layout );
0065 
0066     /**
0067      * Replace the current layout with a new one and also store changes to disk.
0068      * Also use the new layout as current.
0069      * @param layout The updated layout.
0070      */
0071     void updateCurrentLayout( const PlaylistLayout &layout );
0072     
0073     /**
0074      * Get the layout with a specific name. Returns an empty layout if there is no layout matching the name.
0075      * @param layout The name of the layout.
0076      * @return The layout matching the name.
0077      */
0078     PlaylistLayout layout( const QString &layout ) const;
0079     
0080     /**
0081      * Get the currently active layout.
0082      * @return the layout that is currently active.
0083      */
0084     PlaylistLayout activeLayout() const;
0085     
0086     /**
0087      * Get the name o the currently active layout.
0088      * @return The name of the layout that is currently active.
0089      */
0090     QString activeLayoutName() const;
0091 
0092     /**
0093      * Check if a named layout if one of Amarok's defaults or is one added by a user, This is important
0094      * as default layouts cannot be changed or deleted.
0095      * @param layout The name of the layout.
0096      * @return Is layout one of the defaults.
0097      */
0098     bool isDefaultLayout( const QString &layout ) const;
0099 
0100     /**
0101      * Add and store a new user defined layout.
0102      * @param name The name of the new layout.
0103      * @param layout The new layout. 
0104      */
0105     void addUserLayout( const QString &name, PlaylistLayout layout );
0106     
0107     /**
0108      * Delete a layout. Checks if the layout is editable (is not one of the defaults) and deletes it if possible
0109      * @param layout The name of the layout to delete.
0110      */
0111     void deleteLayout( const QString &layout );
0112     
0113     /**
0114      * Check if a layout can be deleted (is not a default layout).
0115      * @param layout The name of the layout.
0116      * @return Can this layout be delete.
0117      */
0118     bool isDeleteable( const QString &layout ) const;
0119 
0120     /**
0121      * Move the named layout up one place if possible
0122      * @param layout The name of he layout to move
0123      * @return The new row of the layout
0124      */
0125     int moveUp( const QString &layout );
0126 
0127     /**
0128      * Move the named layout down one place if possible
0129      * @param layout The name of he layout to move
0130      * @return The new row of the layout
0131      */
0132     int moveDown( const QString &layout );
0133 
0134 Q_SIGNALS:
0135     
0136     /**
0137      * Signal emitted when the active layout changes.
0138      */
0139     void activeLayoutChanged();
0140     
0141     /**
0142      * Signal emitted when the list of layouts has changed. Either because a layout has been added or
0143      * removed or because the order of the layouts has changed.
0144      */
0145     void layoutListChanged();
0146 
0147 protected:
0148     /**
0149      * Private constructor (Singleton Pattern).
0150      */
0151     LayoutManager();
0152 
0153     /**
0154      * Load the default layouts shipped with Amarok.
0155      */
0156     void loadDefaultLayouts();
0157     
0158     /**
0159      * Load user generated layouts.
0160      */
0161     void loadUserLayouts();
0162     
0163     /**
0164      * Order the layouts according to what is stored in the config. If any layouts are present that are not mentioned in the config
0165      * these are added to the bottom of the list, and of any layouts are mentioned in the config that is not found, these are ignored.
0166      */
0167     void orderLayouts();
0168 
0169     /**
0170      * Save the current ordering of the layouts to config.
0171      */
0172     void storeLayoutOrdering();
0173 
0174     /**
0175      * Load all layouts from an XML file.
0176      * @param fileName The file to load the layouts from.
0177      * @param user Are these to be treated as user generated layouts or Amarok default layouts.
0178      */
0179     void loadLayouts( const QString &fileName, bool user );
0180 
0181     /**
0182      * Create a DOM element corresponding to a LayoutItemConfig. Used when storing a layout to a file.
0183      * @param doc The QDomDocument that is the parent of the current tree.
0184      * @param name The name of the layoutItem
0185      * @param item The layout item.
0186      * @return A DOM element containing the XML encoded layout item.
0187      */
0188     QDomElement createItemElement( QDomDocument doc, const QString &name, const LayoutItemConfig &item ) const;
0189     
0190     /**
0191      * Create a LayoutItemConfig based on a QDomElement. Use when reading a layout from an XML file.
0192      * @param elem The DOM element containing the encoded layout item.
0193      * @return The layout item generated.
0194      */
0195     LayoutItemConfig parseItemConfig( const QDomElement &elem ) const;
0196 
0197     static LayoutManager *s_instance;
0198 
0199     QMap<QString, PlaylistLayout> m_layouts;
0200     QStringList                   m_layoutNames;  //used to make a custom ordering of items 
0201     QString                       m_activeLayout;
0202     PlaylistLayout                m_previewLayout;
0203 };
0204 
0205 }
0206 
0207 #endif