File indexing completed on 2024-04-21 09:36:41

0001 /***************************************************************************
0002  *   Copyright (C) 2004 by Juanjo Álvarez Martinez <juanjo@juanjoalvarez.net> *
0003  *   Copyright (C) 2008-2009 by Pino Toscano <pino@kde.org>                *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Steet, Fifth Floor, Boston, MA  02111-1307, USA.          *
0019  ***************************************************************************/
0020 
0021 #ifndef _ALTPARSER_H_
0022 #define _ALTPARSER_H_
0023 
0024 #include <qmetatype.h>
0025 #include <qstringlist.h>
0026 
0027 class Item;
0028 
0029 struct Slave
0030 {
0031     QString slname;
0032     QString slpath;
0033 };
0034 
0035 class Alternative
0036 {
0037     QString m_altPath;
0038     int m_priority;
0039     QString m_description;
0040     Item *m_parent;
0041     QStringList m_altSlaves;
0042 public:
0043     Alternative(Item *parentarg);
0044     Alternative(const Alternative &alt);
0045     ~Alternative();
0046     Alternative& operator=(const Alternative &alt);
0047 
0048     Item* getParent() const { return m_parent; }
0049     QString getPath() const { return m_altPath; }
0050     void setPath(const QString &patharg) { m_altPath = patharg; }
0051     int getPriority() const { return m_priority; }
0052     void setDescription(const QString &desc) { m_description = desc; }
0053     QString getDescription() const { return m_description; }
0054     void setPriority(int priorityarg) { m_priority = priorityarg; }
0055     QStringList getSlaves() const { return m_altSlaves; }
0056     void setSlaves(const QStringList &slaves);
0057     void addSlave(const QString &slave) { m_altSlaves.append(slave); }
0058     int slavesCount() const { return m_altSlaves.count(); }
0059     QString getSlave(int pos) const { return m_altSlaves.at(pos); }
0060     bool isSelected() const;
0061     bool isBroken() const;
0062     bool select(QString *selectError = Q_NULLPTR);
0063 
0064     static QString prettyDescription(Alternative *);
0065 };
0066 
0067 typedef QList<Slave *> SlaveList;
0068 typedef QList<Alternative *> AltsPtrList;
0069 
0070 class Item
0071 {
0072 public:
0073     enum ItemMode
0074     {
0075         AutoMode,
0076         ManualMode
0077     };
0078 private:
0079     QString m_name;
0080     ItemMode m_mode;
0081     QString m_path;
0082     SlaveList *m_itemSlaves;
0083     AltsPtrList *m_itemAlts;
0084 public:
0085     Item();
0086     // Deep copy constructor:
0087     Item(const Item &item);
0088     ~Item();
0089     Item& operator=(const Item &item);
0090 
0091     Alternative* getSelected() const;
0092     QString getName() const { return m_name; }
0093     void setName(const QString &namearg) { m_name = namearg; }
0094     ItemMode getMode() const { return m_mode; }
0095     void setMode(ItemMode modearg) { m_mode = modearg; }
0096     QString getPath() const { return m_path; }
0097     void setPath(const QString &patharg) { m_path = patharg; }
0098     SlaveList *getSlaves() const { return m_itemSlaves; }
0099     void setSlaves(SlaveList *slaves);
0100     void addSlave(const QString &namearg, const QString &patharg);
0101     void delSlave(const QString &namearg);
0102     void delSlaveByPath(const QString &patharg);
0103     AltsPtrList *getAlternatives() const { return m_itemAlts; }
0104     Alternative *getAlternative(const QString &altpath);
0105     void setAlternatives(AltsPtrList &alts);
0106     int countAlternatives() const { return m_itemAlts->count(); }
0107     void delAlternativeByPath(const QString &patharg);
0108     void delAlternativeByPriority(int priorityarg);
0109     void addAlternative(Alternative *altarg) { m_itemAlts->append(altarg); }
0110     bool isBroken() const;
0111 
0112     static QString modeString(ItemMode mode);
0113 };
0114 
0115 typedef QList<Item *> ItemPtrList;
0116 
0117 class AltFilesManager
0118 {
0119     ItemPtrList *m_itemlist;
0120     QString m_altdir;
0121     QString m_errorMsg;
0122     bool m_parseOk;
0123 
0124     bool parseAltFiles(QString &errorstr);
0125 public:
0126     AltFilesManager(const QString &altdir);
0127     ~AltFilesManager();
0128 
0129     ItemPtrList* getGlobalAlternativeList() const { return this->m_itemlist; }
0130     bool parsingOk() const { return m_parseOk; }
0131     QString getErrorMsg() const { return m_errorMsg; }
0132     Item* getItem (const QString &name) const;
0133     //FIXME: Put in a #ifdef
0134     void debugPrintAlts() const;
0135     QString getAltDir() { return m_altdir ;}
0136 };
0137 
0138 Q_DECLARE_METATYPE(Item::ItemMode)
0139 
0140 #endif // _KALTERNATIVES_H_