/***************************************************************************
 *   Copyright (C) 2010 by Volker Lanz <vl@fidra.de>                       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 ***************************************************************************/

#if !defined(CATEGORY__H)

#define CATEGORY__H

#include "core/dbitem.h"
#include "core/types.h"

#include <QString>

class Category;
class Profile;

class QSqlQuery;
class QComboBox;

class Category : public DbItem
{
	public:
		Category(Profile* profile, Category* parent, const QSqlQuery& query);
		explicit Category(Profile* profile = NULL, Category* parent = NULL, const QString& name = QString());
		~Category();

	public:
		bool serialize();
		static bool list(Profile* profile, Category* parent, Categories& result);

		const Profile* profile() const { return m_Profile; }
		Category* parent() const { return m_Parent; }
		const QString& name() const { return m_Name; }

		void setProfile(Profile* p) { m_Profile = p; setDirty(true); }
		void setParent(Category* parent) { m_Parent = parent; setDirty(true); }
		void setName(const QString& n) { m_Name = n; setDirty(true); }

		Categories& subCategories() { return m_SubCategories; }
		const Categories& subCategories() const { return m_SubCategories; }

		static Category* findCategory(quint64 id, Categories& node);
		static void toComboBox(QComboBox* combo, Categories& list, quint32 level = 0);
		static bool indirectlyContains(const Category* parent, const Category* child);

	private:
		Profile* m_Profile;
		Category* m_Parent;
		QString m_Name;
		Categories m_SubCategories;
};

Q_DECLARE_METATYPE(Category*);

#endif
