Sophie

Sophie

distrib > Mandriva > 2009.0 > i586 > by-pkgid > 43ed067ef12dc23f248aedf5d56e3391 > files > 2

libdbmusic-devel-0.8.4-1mdv2008.0.i586.rpm

/***************************************************************************
                         dbmusic.h  -  description
                            -------------------
                  begin                : Wed Dec 19 2001
                  copyright            : (C) 2001 by root
                  email                : badhack@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef DBMUSIC_H
#define DBMUSIC_H

#include "dbmusicglobals.h"
#include <libpq-fe.h>
#include <qlistview.h>
#include <qmap.h>
#include <qtextstream.h>
#include <qfile.h>
#include <kiconloader.h>

/**This is the database interface class. This class provides all the music database functionality
 * through functions. The data type to be modified will be passed const x & and these functions
 * return an integer error code value.
 *
 * There are some standard int values that are used for genres/labels/etc. They are so similar the queries
 * end up nearly identical, so it's easier to pass an int and combine everything into one function.
 * Those values are:
 * @li @p 1-	Boxset
 * @li @p 2-	Format
 * @li @p 3-	Genre
 * @li @p 4-	Label
 * @li @p 5-	Method
 * @li @p 6-	Type
 * @li @p 7-	Bands To Explore (if implemented)
 *
 * Instead of using integers values, I have included some #define's to make it easier:
 * @li @p 0-	KBOXSET
 * @li @p 1-	KFORMAT
 * @li @p 2-	KGENRE
 * @li @p 3-	KLABEL
 * @li @p 4-	KMETHOD
 * @li @p 5-	KTYPE
 * @li @p 6-	KBTE
 * @author Will DeRousse
 * @short Music database query, add, delete and update functionality.
 */

class dbMusic {
	friend class dbReport;
	public:
		/** Constructor.
		 * The connection to the backend is made, and all the QMap caches are filled.
		 * <pre>
		 * dbMusic *music=new dbMusic("dbname");
		 * music->addArtist(x);
		 * delete music;
		 * </pre>
		 *
		 * Initializing a dbMusic object makes the connect. Deleting the object breaks the connect.
		 * @param string PostgreSQL style connection string
		 */
		dbMusic(const QString &);
		/**
		 * Destructor
		 */
		~dbMusic();
		/** Adds new artist to the db.
		 *
		 * Returns error code.
		 * @param artistInfo Pointer to the artist info struct
		 */
		int addArtist(artistInfo *const);
		/** Adds a category to the database.
		 *
		 * Returns error code.
		 * @param categoryInfo Pointer to category info struct
		 */
		int addCategory(categoryInfo *const);
		/** Adds the title to the db.
		 *
		 * Returns error code.
		 * @param titleInfo Pointer to the title information
		 * @param QString AID
		 */
		int addTitle(titleInfo *const, const QString &);
		/** Adds a track to the database.
		 *
		 * Returns error code.
		 * @param trackInfo Pointer to trackInfo struct containing track description
		 * @param QString CDID
		 */
		int addTrack(trackInfo *const, const QString &);
		/** Creates a pg_dump compatible file.
		 *
		 * Returns error code.
		 * @param string Name Of File
		 */
		int backup(const QString &);
		/** Checks the connection with the database to insure that it is ok.
		 *
		 * Returns error code.
		 */
		bool checkConnect( ) const;
		/** Retrieves the database version this library was compiled for and sets the QString passed.
		 *
		 * Returns the error code.
		 * @param QString Version
		 */
		int dbVersionCompat(QString &);
		/** Retrieves the currently opened database version and sets the QString passed.
		 *
		 * Returns the error code.
		 * @param QString Version
		 */
		int dbVersionLive(QString &);
		/** Deletes artist from the database.
		 *
		 * Returns error code.
		 * @param QString AID
		 */
		int deleteArtist(const QString &);
		/** Deletes a specific category entry from the database.
		 *
		 * Returns error code.
		 * @param categoryInfo Pointer to category info struct
		 */
		int deleteCategory(const categoryInfo *const);
		/** Deletes title (and subsequent tracks) from the db.
		 *
		 * Returns error code.
		 * @param QString CDID Index Number
		 */
		int deleteTitle(const QString &);
		/** Deletes a track from the database.
		 *
		 * Returns error code.
		 * @param QString Track ID
		 * @param QString CDID
		 */
		int deleteTrack(const QString &, const QString &);
		/** Returns last error message from the server.
		 *
		 * Returns error code.
		 * @param QString Error string to set.
		 */
		int errorMessage(QString &);
		/**
		 * "formats" the database out for use with dbMusic. It lays out all the basic tables, indexes, sequences,  and triggers for referential integrity.
		 * Also adds a few basic entries to the categories. For more details, see dbmusic.dump or kmusicdb manual.
		 *
		 * Returns error code.
		 */
		int formatDb();
		/** Retrieves aid by name.
		 *
		 * Returns error code.
		 * @param QString AID
		 * @param QString Artist Name
		 */
		int getAid(QString &, const QString &);
		/** Fills struct with a particular artist.
		 * You must set aid within the struct first.
		 *
		 * Returns error code.
		 * @param artistInfo Struct to be filled
		 */
		int getArtist(artistInfo *const);
		/** Fills struct with the info on a category. You must set xid first.
		 *
		 * @param categoryInfo Pointer To Struct
		 */
		int getCategory(categoryInfo *const);
		/** Retrieves an image from the database. NOT IMPLEMENTED.
		 * @param QString CDID
		 * @param char image data
		 * @param int image size
		 */
		int getImage(const QString &, char *, int &);
		/** Retrieves the lyrics from a track in the database.
		 *
		 * @param trackInfo Pointer To Struct
		 */
		int getLyric(trackInfo *const);
		/** Fills struct with the info on a title.
		 * You must set cdid within the struct first.
		 *
		 * Returns error code.
		 * @param titleInfo Struct to be filled
		 */
		int getTitle(titleInfo *const);
		/** Fills struct with the into on a track.
		 *
		 * @param trackInfo Pointer To Struct
		 */
		int getTrack(trackInfo *const);
		/** Runs VACUUM (optimizer/defrag) on the database.
		 *
		 * Returns error code.
		 */
		int opti();
		/** Populates the QListView with artist information.
		 * AID is added to the first column after last enabled.
		 *
		 * @param QListView Pointer to listview to be populated
		 * @param QString AID
		 * @param enabledArtistColumns Pointer to column display information
		 */
		int populateArtistQListView(QListView *const, const QString &, const enabledArtistColumns *const);
		/** Populates the category QListView with the names of the chosen category.
		 *
		 * @param QListView Pointer to QListView
		 * @param int Category type
		 * @param enabledCategoryColumns Pointer to column display information
		 */
		int populateCategoryQListView(QListView *const, const int &, const enabledCategoryColumns *const);
		/** Fills the QStringList with category information.
		 *
		 * @param QStringList Pointer to the list to be filled
		 * @param int Which value to populate (genre, label, etc.)
		 */
		int populateCategoryQStringList(QStringList *const, const int &);
		/**
		 * Populates the QStringList with either artist, titles or tracks depending on ctype. The filter is
		 * an AID (for titles) or CDID (for tracks). It is ignored for artists. If a value of 0 or less is passed
		 * ALL values are returned.
		 *
		 * List is sorted alphabetically.
		 *
		 * @param QStringList Pointer to list to be populated
		 * @param int Type of List: ARTIST, TITLE, TRACK
		 * @param QString Filter
		 */
		int populateQStringList(QStringList *const, const int &, const QString &);
		/** Populates the QListView with title information.
		 * The CDID is set to the right of the last enabled column.
		 *
		 * Returns error code.
		 * @param QListView Pointer to QListView to be populated
		 * @param QString AID
		 * @param enabledTitleOptions Pointer to struct containing column information
		 */
		int populateTitleQListView(QListView *const, const QString &, const enabledTitleColumns *const);
		/** Populates a QListView with the track information of a specified CDID.
		 * The TRACKID is set to the right of the last enabled column.
		 *
		 * Returns error code.
		 * @param QListView Pointer To QListView
		 * @param QString CDID
		 * @param enabledTrackColumns Pointer to struct containing column information
		 */
		int populateTrackQListView(QListView *const, const QString &, const enabledTrackColumns *const);
		/** Restores the db from a PostgreSQL dump file.
		 *
		 * Returns error code.
		 * @param string Filename To Restore
		 */
		int restore(const QString &);
		/** Tests the connection with the db to insure that it is reliable.
		 *
		 * Returns error code.
		 */
		int testConnect();
		/** Sets the QString to the total.
		 *
		 * Returns error code.
		 * @param QString Pass a string to have its value set
		 * @param int Group to be summed: ARTIST,TITLE,TRACK
		 * @param int Filter: REPORT_ALL,REPORT_YES,REPORT_NO
		 */
		int total(QString &, const int &, const int &);
		/** Sets QString to total titles/tracks by artist/title.
		 *
		 * Returns error code.
		 * @param QString Value that gets set
		 * @param QString ID
		 * @param int Group to be summed: ARTIST,TITLE
		 * @param int Filter: REPORT_ALL, REPORT_YES, REPORT_NO
		 */
		int totalBy(QString &, const QString &, const int &, const int &);
		/** Sets the number of categories by the current artist.
		 *
		 * Returns error code.
		 * @param QString Value that gets set
		 * @param int Category Type
		 */
		int totalCategory(QString &, const int &);
		/** Sets the total length of the matched parameters.
		 *
		 * Returns error code.
		 * @param QString This will get set to the number
		 * @param QString ID: AID, CDID, 0 (for all tracks)
		 * @param QString Group to be summed: ARTIST,TITLE,TRACK
		 */
		int totalLength(QString &, const QString &, const int &);
		/** Moves a track down one number for a specific title.
		 *
		 * Returns error code.
		 * @param trackInfo Top Track
		 * @param trackInfo Bottom Track
		 */
		int trackDown(const trackInfo *const, const trackInfo *const);
		/** Moves a track up one number for a specific title.
		 *
		 * Returns error code.
		 * @param trackInfo Top Track
		 * @param trackInfo Bottom Track
		 */
		int trackUp(const trackInfo *const, const trackInfo *const);
		/** Updates a particular artist.
		 * You must set aid within the struct first.
		 *
		 * Returns error code.
		 * @param artistInfo Pointer to artist information
		 */
		int updateArtist(const artistInfo *const);
		/** Updates a specific category entry.
		 *
		 * Returns error code.
		 * @param categoryInfo Pointer to struct with XID and new name.
		 */
		int updateCategory(const categoryInfo *const);
		/** Updates an image. NOT IMPLEMENTED.
		 * @param QString CDID
		 * @param char image data
		 * @param int image size
		 */
		int updateImage(const QString &, const char *, const int &);
		/** Updates the lyric for an existing track.
		 *
		 * @param trackInfo Pointer To Struct
		 */
		int updateLyric(const trackInfo *const);
		/** Updates the title.
		 * You must set cdid within the struct first.
		 *
		 * Returns error code.
		 * @param titleInfo Pointer to title information
		 */
		int updateTitle(const titleInfo *const);
		/** Updates a specific track.
		 *
		 * Returns error code.
		 * @param trackInfo Pointer to trackInfo struct containing track ID to update as well as the updated information
		 */
		int updateTrack(const trackInfo *const);
	private:
		/**
		 * Support function for backup. Help reduce redundant code.
		 *
		 * Returns error code.
		 * @param QString Id Type
		 * @param QString Table Name
		 */
		int backupTable(const QString &, const QString &);
		/**
		 * Support function for backup. Reduces redundant code.
		 *
		 * @param QString Table Name
		 */
		void writeTable(const QString &);
		/** Checks the result of the query or whatever and returns the error code.
		 *
		 * Returns error code.
		 */
		int execCheck() const;
		/** Updates the length (title.cdlen) of the title.
		 *
		 * Returns error code.
		 * @param QString CDID
		 */
		int updateTime(const QString &);
		/** Wrapper to help handle local character sets.
		 *
		 * Returns QString.
		 *
		 * @param PGresult Query result
		 * @param int tuple number
		 * @param int field number
		 */
		QString GetValue(const PGresult *res, int tup_num, int field_num);
		ConnStatusType stat;
		PGconn *dbm;
		PGresult *res;
		int error_value;
		QFile *htmlrep;
		QString sqlstring;
		QTextStream *s;
};


/**
 * This is the reporting class for libdbmusic. Pass a connection (dbMusic class) as a parameter and run existing reports or create your own.
 * @short Reporting class for libdbmusic.
 */
class dbReport
{
	public:
		/** Constructor
		 * This is the primary reporting class. It comes with several "generic" HTML functions that can be used to design a W3C compliant report. It also comes with several "generic" SQL functions that allow you to construct a query to the server. This class can be easily inherited to provide your own reports.
		 *
		 * @param dbMusic Pointer to dbMusic database connection.
		 */
		dbReport(dbMusic *);
		/** Destructor
		 */
		~dbReport();
		/**
		 * Returns struct with values set that are supported for particular report.
		 * @param int Report Type
		 */
		static reportProvides dbreportProvides(const int &);
		/**
		 * Generates an HTML report.
		 *
		 * Returns error code.
		 * @param QString HTML String
		 * @param reportInfo reportInfo struct containing report variables
		 * @param colorInfo colorInfo struct containing color variables
		 */
		int artistReport(QString &, const reportInfo *const, const colorInfo *const);
		/**
		 * Generates an HTML report.
		 *
		 * Returns error code.
		 * @param QString HTML String
		 * @param reportInfo reportInfo struct containing report variables
		 * @param colorInfo colorInfo struct containing color variables
		 */
		int bteReport(QString &, const reportInfo *const, const colorInfo *const);
		/**
		 * Generates an HTML report.
		 *
		 * Returns error code.
		 * @param QString HTML String
		 * @param reportInfo reportInfo struct containing report variables
		 * @param colorInfo colorInfo struct containing color variables
		 * @param int Report Type (Type, Label, Genre, etc)
		 * @param QString Value
		 */
		int categorySearchReport(QString &, const reportInfo *const, const colorInfo *const, const int &, const QString &);
		/**
		 * Generates an HTML report.
		 *
		 * Returns error code.
		 * @param QString HTML String
		 * @param reportInfo reportInfo struct containing report variables
		 * @param colorInfo colorInfo struct containing color variables
		 */
		int collectionReport(QString &, const reportInfo *const, const colorInfo *const);
		/**
		 * Generates an HTML report.
		 *
		 * Returns error code.
		 * @param QString HTML String
		 * @param reportInfo reportInfo struct containing report variables
		 * @param colorInfo colorInfo struct containing color variables
		 * @param QString Custom report string
		 */
		int customReport(QString &, const reportInfo *const, const colorInfo *const, const QString &);
		/**
		 * Generates an HTML report.
		 *
		 * Returns error code.
		 * @param QString HTML String
		 * @param reportInfo reportInfo struct containing report variables
		 * @param colorInfo colorInfo struct containing color variables
		 */
		int dateRangeReport(QString &, const reportInfo *const, const colorInfo *const);
		/**
		 * Generates an HTML report.
		 *
		 * Returns error code.
		 * @param QString HTML String
		 * @param reportInfo reportInfo struct containing report variables
		 * @param colorInfo colorInfo struct containing color variables
		 * @param QString Value
		 * @param report Type (artist, title, track)
		 */
		int nameSearchReport(QString &, const reportInfo *const, const colorInfo *const, const QString &, const int &);
		/**
		 * Generates an HTML report.
		 *
		 * Returns error code.
		 * @param QString HTML String
		 * @param reportInfo reportInfo struct containing report variables
		 * @param colorInfo colorInfo struct containing color variables
		 */
		int statisticsReport(QString &, const reportInfo *const, const colorInfo *const);
		/**
		 * Generates an HTML report.
		 *
		 * Returns error code.
		 * @param QString HTML String
		 * @param reportInfo reportInfo struct containing report variables
		 * @param colorInfo colorInfo struct containing color variables
		 * @param QString AID
		 */
		int titlesByArtist(QString &, const reportInfo *const, const colorInfo *const, const QString &);
		/**
		 * Generates an HTML report.
		 *
		 * Returns error code.
		 * @param QString HTML String
		 * @param reportInfo reportInfo struct containing report variables
		 * @param colorInfo colorInfo struct containing color variables
		 */
		int topTitlesReport(QString &, const reportInfo *const, const colorInfo *const);
		/**
		 * Generates an HTML report.
		 *
		 * Returns error code.
		 * @param QString HTML String
		 * @param reportInfo reportInfo struct containing report variables
		 * @param colorInfo colorInfo struct containing color variables
		 * @param QString CDID
		 */
		int tracksByTitle(QString &, const reportInfo *const, const colorInfo *const, const QString &);

		/**
		 * Generates BODY tag.
		 * @param QString String To Append
		 */
		void HTMLBody(QString &);
		/**
		 * Generates column headers.
		 * @param QString String To Append
		 * @param QStringList Color List
		 */
		void HTMLColumnHeaders(QString &, const QStringList *const);
		/**
		 * Inserts CSS information into the document. MUST be used or document formatting will not occur.
		 * @param QString String To Append
		 * @param QString Custom CSS To Add
		 * @param reportInfo reportInfo Struct
		 * @param colorInfo colorInfo Struct
		 */
		void HTMLCSS(QString &, const QString &, const reportInfo *const, const colorInfo *const);
		/**
		 * Generates DOCTYPE and charset information.
		 * @param QString String To Append
		 * @param QString Language
		 */
		void HTMLDocType(QString &, const QString &);
		/**
		 * Generates HEAD and META information for reports.
		 * Also sets charset of report and inserts the CSS information.
		 * @param QString String To Append
		 * @param QString Report Title
		 * @param QString Metaname
		 * @param QString Report Version
		 * @param reportInfo reportInfo Struct
		 * @param colorInfo colorInfo Struct
		 */
		void HTMLHeader(QString &, const QString &, const QString &, const QString &, const reportInfo *const, const colorInfo *const);
		/**
		 * Populates the QString with table row data from the server.
		 * @param QString String To Append
		 * @param QStringList List Of Column Names
		 * @param int > 0 Splits data into two columns, else one column
		 */
		void HTMLPopulateData(QString &, const QStringList *const, const int &);
		/**
		 * A function for statistics to reduce code base.
		 * Populates with category information.
		 * @param QString String To Append
		 * @param float Denominator
		 * @param QStringList Color List
		 */
		void HTMLPopulateData2(QString &, const float &, const QStringList *const);
		/**
		 * Generates a simple table header (only colspan's and includes a title).
		 * @param QString String To Append
		 * @param QString Number Of Columns
		 * @param QString Table Name
		 */
		void HTMLSimpleTableHeader(QString &, const QString &, const QString &);
		/**
		 * Fills in a sum header for the end of a report.
		 * @param QString String To Append
		 * @param QString Colspan
		 * @param QString Name
		 * @param QString Value
		 */
		void HTMLSumHeader(QString &, const QString &, const QString &, const QString &);
		/**
		 * Appends the date, user, dbname information.
		 * @param QString String To Append
		 * @param QString Database Name
		 */
		void HTMLSysInfo(QString &, const QString &);
		/**
		 * Starts a new table.
		 * @param QString String To Append
		 */
		void HTMLTable(QString &);
		/**
		 * Generates Full Report Header (including information about Date Range, Collected, etc).
		 * @param QString String To Append
		 * @param QString Number Of Columns
		 * @param QString Report Name
		 * @param reportInfo reportInfo Struct
		 * @param reportProvides reportProvides Struct
		 */
		void HTMLTableHeader(QString &, const QString &, const QString &, const reportInfo *const, const reportProvides *const);
		/**
		 * Used to add a single contraint to a query in the form of x=y.
		 * Used to add the collect/follow syntax to a query.
		 * @param QString String To Append
		 * @param QString Value
		 * @param reportInfo reportInfo Struct
		 */
		void queryBoolStatus(QString &, const QString &, const reportInfo *const);
		/**
		 * Adds a limit to the query.
		 * @param QString String To Append
		 * @param reportInfo reportInfo Struct
		 */
		void queryLimit(QString &, const reportInfo *const);
		/**
		 * Adds matching for two fields (a.name=t.name).
		 * @param QString String To Append
		 * @param QString First Value
		 * @param QString Second Value
		 */
		void queryMatch(QString &, const QString &, const QString &);
		/**
		 * Adds ORDER BY syntax to a query.
		 * @param QString String To Append
		 * @param QStringList Order List
		 * @param reportInfo reportInfo Struct
		 */
		void queryOrderBy(QString &, const QStringList *const, const reportInfo *const);
		/**
		 * Adds the date range syntax to a query.
		 * @param QString String To Append
		 * @param QString Begin
		 * @param QString End
		 * @param reportInfo reportInfo Struct
		 */
		void queryRange(QString &, const QString &, const QString &, const reportInfo *const);
	private:
		/** Checks the result of the query or whatever and returns the error code.
		 *
		 * Returns error code.
		 */
		int execCheck() const;
		/** Wrapper to help handle local character sets.
		 *
		 * Returns QString.
		 *
		 * @param PGresult Query result
		 * @param int tuple number
		 * @param int field number
		 */
		QString GetValue(const PGresult *res, int tup_num, int field_num);
		QString sqlstring, tnod, tlen;
		ExecStatusType stat;
		int error_value;
		dbMusic *music;
		QStringList *list;
};

#endif