summaryrefslogtreecommitdiff
path: root/mg_db.h
blob: 40e33083ebf770351ceffdeb87a221cf4e4354cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
/*!
 * \file mg_db.h
 * \brief A database interface to the GiantDisc
 *
 * \version $Revision: 1.0 $
 * \date    $Date: 2004-12-07 10:10:35 +0200 (Tue, 07 Dec 2004) $
 * \author  Wolfgang Rohdewald
 * \author  Responsible author: $Author: wr $
 *
 */

#ifndef _DB_H
#define _DB_H
#include <stdlib.h>
#include <mysql/mysql.h>
#include <string>
#include <list>
#include <vector>
#include <map>
#include <iostream>
#include <istream>
#include <sstream>
#include <ostream>
#include <i18n.h>
using namespace std;

#include "mg_tools.h"

typedef vector<string> strvector;

//! \brief a map for reading / writing configuration data. 
class mgValmap : public map<string,string> {
	private:
		const char *m_key;
	public:
		/*! \brief constructor
		 * \param key all names will be prefixed with key.
		 */
		mgValmap(const char *key);
		//! \brief read from file
		void Read(FILE *f);
		//! \brief write to file
		void Write(FILE *f);
		//! \brief enter a string value
		void put(const char*name, string value);
		//! \brief enter a C string value
		void put(const char*name, const char* value);
		//! \brief enter a long value
		void put(const char*name, long value);
		//! \brief enter a int value
		void put(const char*name, int value);
		//! \brief enter a unsigned int value
		void put(const char*name, unsigned int value);
		//! \brief enter a bool value
		void put(const char*name, bool value);
	 //! \brief return a string
	 string getstr(const char* name) {
		 return (*this)[name];
	 }
	 //! \brief return a C string
	 bool getbool(const char* name) {
		 return (getstr(name)=="true");
	 }
	 //! \brief return a long
	 long getlong(const char* name) {
		 return atol(getstr(name).c_str());
	 }
	 //! \brief return an unsigned int
	 unsigned int getuint(const char* name) {
		 return (unsigned long)getlong(name);
	 }
};

static const string EMPTY = "XNICHTGESETZTX";

class mgSelection;

//! \brief a generic keyfield
class keyfield
{
    public:
        //! \brief set the owning selection. 
        void setOwner(mgSelection *owner) { selection = owner; }

	//! \brief default constructor
        keyfield ()
        {
        };

	/*! \brief constructs a simple key field which only needs info from
 	 * the tracks table.
 	 * \param choice the internationalized name of this key field, e.g. "Jahr"
 	 */
        keyfield (const string choice);

	//! \brief default destructor
        virtual ~ keyfield ()
        {
        };

/*! \brief assigns a new id and value to the key field.
 * This also invalidates the cache of the owning mgSelection.
 * \param id used for lookups in the data base
 * \param value used for display
 */
        void set (const string id, const string value);

//! \brief helper function for streaming debug info
        void writeAt (ostream &) const;

//! \brief adds lookup data to a WHERE SQL statement
        string restrict (string & result) const;

//! \brief returns the internationalized name of this key field
        string choice () const
        {
            return m_choice;
        }

/*! \brief returns the id of this key field. This is the string used
 * for lookups in the data base, not for display
 */
        string id () const
        {
            return m_id;
        }

/*! \brief returns the filter for this key field.
 * \todo filters are not yet implemented but we already dump them in DumpState
 */
        string filter () const
        {
            return m_filter;
        }

/*! \brief returns the value of this key field. This is the string used
 * for the display.
 */
        string value () const
        {
            return m_value;
        }

	virtual string order() const
	{
	    return valuefield ();
	}

//! \brief returns the name of the corresponding field in the tracks table
        virtual string basefield () const { return ""; }

//! \brief returns the name of the field to be shown in the selection list
        virtual string valuefield () const
        {
            return basefield ();
        }

//! \brief returns the name of the identification field
        virtual string idfield () const
        {
            return basefield ();
        }

/*! \brief returns the name of the field needed to count how many
 * different values for this key field exist
 */
        virtual string countfield () const
        {
            return basefield ();
        }

/*! \brief returns a join clause needed for the composition of
 * a WHERE statement
 */
        virtual string join () const;

/*! \brief returns a join clause needed for the composition of
 * a WHERE statement especially for counting the number of items
 */
        virtual string countjoin () const
        {
            return join ();
        }

/*! \brief if true, the WHERE clause should also return values from
 * join tables
 */
        bool lookup;

//! \brief returns a SQL query command for counting different key
//values
        string KeyCountquery ();

    protected:
//! \brief the owning selection. 
	mgSelection* selection;
	//! \brief the english name for this key field
        string m_choice;
	//! \brief used for lookup in the data base
        string m_id;
	//! \brief used for OSD display
        string m_value;
	//! \brief an SQL restriction like 'tracks.year=1982'
        string m_filter;
	/*! \brief should be defined as true if we need to join another
	 * table for getting user friendly values (like the name of a genre)
	 */
        virtual bool need_join () const;
	//! \brief escape the string as needed for calls to mysql
	string sql_string(const string s) const;
};


//! \brief orders by collection
class collectionkeyfield:public keyfield
{
    public:
        collectionkeyfield ():keyfield ("Collection")
        {
        }
        string basefield () const
        {
            return "playlist.id";
        }
        string valuefield () const
        {
            return "playlist.title";
        }
/* this join() would ensure that empty collections be suppressed. But we
 * want them all. so we don't need the join
 */
        string join () const
        {
            return "";
        }
};

//! \brief orders by position in collection
class collectionitemkeyfield:public keyfield
{
    public:
        collectionitemkeyfield ():keyfield ("Collection item")
        {
        }
        string basefield () const
        {
            return "playlistitem.tracknumber";
        }
        string valuefield () const
        {
            return "tracks.title";
        }
	string order () const
	{
	    return basefield ();
	}
        string join () const
        {
            return
                "tracks.id=playlistitem.trackid and playlist.id=playlistitem.playlist";
        }
};

//! \brief orders by album.title
class albumkeyfield:public keyfield
{
    public:
        albumkeyfield ():keyfield ("Album")
        {
        }
        string basefield () const
        {
            return "tracks.sourceid";
        }
        string valuefield () const
        {
            return "album.title";
        }
        string idfield () const
        {
            return "album.title";
        }
        string countfield () const
        {
            return "album.title";
        }
        string join () const
        {
            return "tracks.sourceid=album.cddbid";
        }
    protected:
        //!brief we always need to join table album
        bool need_join () const
        {
            return true;
        };
};

//! \brief orders by genre1
class genre1keyfield:public keyfield
{
    public:
        genre1keyfield ():keyfield ("Genre 1")
        {
        }
        string basefield () const
        {
            return "tracks.genre1";
        }
        string valuefield () const
        {
            return "genre.genre";
        }
        string idfield () const
        {
            return "genre.id";
        }
};

//! \brief orders by genre2
class genre2keyfield:public keyfield
{
    public:
        genre2keyfield ():keyfield ("Genre 2")
        {
        }
        string basefield () const
        {
            return "tracks.genre2";
        }
        string valuefield () const
        {
            return "genre.genre";
        }
        string idfield () const
        {
            return "genre.id";
        }
};

//! \brief orders by language
class langkeyfield:public keyfield
{
    public:
        langkeyfield ():keyfield ("Language")
        {
        }
        string basefield () const
        {
            return "tracks.lang";
        }
        string valuefield () const
        {
            return "language.language";
        }
        string idfield () const
        {
            return "language.id";
        }
};

//! \brief orders by tracks.artist
class artistkeyfield:public keyfield
{
    public:
        artistkeyfield ():keyfield ("Artist")
        {
        }
        string basefield () const
        {
            return "tracks.artist";
        }
};

//! \brief orders by tracks.rating
class ratingkeyfield:public keyfield
{
    public:
        ratingkeyfield ():keyfield ("Rating")
        {
        }
        string basefield () const
        {
            return "tracks.rating";
        }
};

//! \brief orders by tracks.year
class yearkeyfield:public keyfield
{
    public:
        yearkeyfield ():keyfield ("Year")
        {
        }
        string basefield () const
        {
            return "tracks.year";
        }
};

//! \brief orders by tracks.title
class titlekeyfield:public keyfield
{
    public:
        titlekeyfield ():keyfield ("Title")
        {
        }
        string basefield () const
        {
            return "tracks.title";
        }
};

//! \brief orders by tracks.tracknb and tracks.title
class trackkeyfield:public keyfield
{
    public:
        trackkeyfield ():keyfield ("Track")
        {
        }
        string basefield () const
        {
            return "tracks.tracknb";
        }
        string valuefield () const
        {
            return
                "concat("
		  "if(tracks.tracknb>0,"
		    "concat("
		      "if(tracks.tracknb<10,'  ',''),"
		      "tracks.tracknb,"
		      "' '"
		    "),''"
		  "),"
		"tracks.title)";
        }
};

//! \brief orders by decade (deduced from tracks.year)
class decadekeyfield:public keyfield
{
    public:
        decadekeyfield ():keyfield ("Decade")
        {
        }
        string basefield () const
        {
            return "substring(convert(10 * floor(tracks.year/10), char),3)";
        }
};

//! \brief represents a content item like an mp3 file
class mgContentItem
{
    public:
        mgContentItem ()
        {
        }
	//! \brief copy constructor
        mgContentItem(const mgContentItem* c);

	//! \brief construct an item from an SQL row
        mgContentItem (const MYSQL_ROW row, const string ToplevelDir);
//! \brief returns track id
        long getId () const
        {
            return m_id;
        }

//! \brief returns title
        string getTitle () const
        {
            return m_title;
        }

//! \brief returns filename
        string getSourceFile () const
        {
            return m_mp3file;
        }

//! \brief returns artist
        string getArtist () const
        {
            return m_artist;
        }

//! \brief returns the name of the album
        string getAlbum ();

//! \brief returns the name of genre 1
        string getGenre1 ();

//! \brief returns the name of genre 2
        string getGenre2 ();

//! \brief returns the name of genre 1
        string getGenre ()
        {
            return getGenre1 ();
        }

//! \brief returns the bitrate
        string getBitrate () const
        {
            return m_bitrate;
        }

//! \brief returns the file name of the album image
        string getImageFile ();

//! \brief returns year
        int getYear () const
        {
            return m_year;
        }

//! \brief returns rating
        int getRating () const
        {
            return m_rating;
        }

//! \brief returns duration
        int getDuration () const
        {
            return m_duration;
        }

//! \brief returns samplerate
        int getSampleRate () const
        {
            return m_samplerate;
        }

//! \brief returns # of channels
        int getChannels () const
        {
            return m_channels;
        }
    private:
        long m_id;
        string m_title;
        string m_mp3file;
        string m_artist;
        string m_albumtitle;
        string m_genre1;
        string m_genre2;
        string m_bitrate;
        int m_year;
        int m_rating;
        int m_duration;
        int m_samplerate;
        int m_channels;
};

/*!
 * \brief the only interface to the database.
 */
class mgSelection
{
    private:
	class mgSelStrings 
	{
		friend class mgSelection;
		private:
			strvector strings;
			mgSelection* m_sel;
			void setOwner(mgSelection* sel);
		public:
			string& operator[](unsigned int idx);
			size_t size();
	};

    public:
/*! \brief define various ways to play music in random order
 * \todo Party mode is not implemented, does same as SM_NORMAL
 */
        enum ShuffleMode
        {
            SM_NONE,                              //!< \brief play normal sequence
            SM_NORMAL,                            //!< \brief a shuffle with a fair distribution
            SM_PARTY                              //!< \brief select the next few songs randomly, continue forever
        };

//! \brief define various ways to play music in a neverending loop
        enum LoopMode
        {
            LM_NONE,                              //!< \brief do not loop
            LM_SINGLE,                            //!< \brief loop a single track
            LM_FULL                               //!< \brief loop the whole track list
        };

//! \brief escapes special characters
	string sql_string(const string s) const;

//! \brief the default constructor. Does not start a DB connection.
        mgSelection();

/*! \brief the main constructor
 * \param Host where the data base lives. If not localhost, TCP/IP is used.
 * \param User if empty, the current user is used.
 * \param Password no comment
 * \param fall_through if TRUE: If enter() returns a choice
 * containing only one item, that item is automatically entered.
 * The analog happens with leave()
 */
        mgSelection (const string Host, const string User =
            "", const string Password = "", const bool fall_through =
            false);

/*! \brief a copy constructor. Does a deep copy.
 * Some of the data base content will only be retrieved by the
 * new mgSelection as needed, so some data base
 * overhead is involved
 */
        mgSelection (const mgSelection& s);
/*! \brief a copy constructor. Does a deep copy.
 * Some of the data base content will only be retrieved by the
 * new mgSelection as needed, so some data base
 * overhead is involved
 */
        mgSelection (const mgSelection* s);

/*! \brief the assignment operator. Does a deep copy.
 * Some of the data base content will only be retrieved by the
 * new mgSelection as needed, so some data base
 * overhead is involved
 */
	const mgSelection& operator=(const mgSelection& s);

//! \brief initializes from a map.
	void InitFrom(mgValmap& nv);

//! \brief the normal destructor
        ~mgSelection ();

	//! \brief sets the top level directory where content is stored
	void setToplevelDir(string ToplevelDir) { m_ToplevelDir = ToplevelDir; }

/*! \brief represents all values for the current level. The result
 * is cached in values, subsequent accesses to values only incur a
 * small overhead for building the SQL WHERE command. The values will
 * be reloaded when the SQL command changes
 * \todo we should do more caching. The last 5 result sets should be cached.
 */
        mgSelStrings values;

/*! \brief defines a field to be used as key for selection
 *
 * \param level 0 is the top level
 * \param name of the key field, internationalized. Possible values
 * are defined by keychoice()
 */
        void setKey (const unsigned int level, const string name);

/*! \brief returns the name of a key
 */
        string getKeyChoice (const unsigned int level)
        {
            return keys[level]->choice ();
        }
	//! \brief return the current value of this key
        string getKeyValue (const unsigned int level)
        {
            return keys[level]->value ();
        }

//! \brief returns a map (new allocated) for all used key fields and their values
	map<string,string> * UsedKeyValues();

//! \brief helper function for << operator (dumps debug info)
        void writeAt (ostream &);

/*! \brief returns FROM and WHERE clauses for the current state
 * of the selection.
 * \param want_trackinfo work in progress, should disappear I hope
 */
        string where (bool want_trackinfo = false);

//! \brief the number of music items currently selected
        unsigned int count ();

//! \brief the number of key fields used for the query
        unsigned int size ();

//! \brief the current position in the current level
        unsigned int gotoPosition () 
        {
            return gotoPosition (m_level);
        }

//! \brief the current position
        unsigned int getPosition (unsigned int level) const;

	//! \brief go to the current position. If it does not exist,
	// go to the nearest.
        unsigned int gotoPosition (unsigned int level);

//! \brief the current position in the tracks list
        unsigned int getTrackPosition () const;

	//! \brief go to the current track position. If it does not exist,
	// go to the nearest.
        unsigned int gotoTrackPosition ();

/*! \brief enter the the next higher level, go one up in the tree.
 * If fall_through (see constructor) is set to true, and the
 * level entered by enter() contains only one item, automatically
 * goes down further until a level with more than one item is reached.
 * \param position is the position in the current level that is to be expanded
 * \return returns false if there is no further level
 */
        bool enter (unsigned int position);

	/*! \brief like enter but if we are at the leaf level simply select
	 * the entry at position
	 */
        bool select (unsigned int position);

/*! \brief enter the next higher level, expanding the current position.
 * See also enter(unsigned int position)
 */
        bool enter ()
        {
            return enter (gotoPosition ());
        }

	/*! \brief like enter but if we are at the leaf level simply select
	 * the current entry
	 */
        bool select ()
        {
            return select (gotoPosition ());
        }

/*! \brief enter the next higher level, expanding the position holding a certain value
 * \param value the position holding value will be expanded.
 */
        bool enter (const string value)
        {
            return enter (valindex (value));
        }

	/*! \brief like enter but if we are at the leaf level simply select
	 * the current entry
	 */
        bool select (const string value)
        {
            return select (valindex(value));
        }

/*! \brief leave the current level, go one up in the tree.
 * If fall_through (see constructor) is set to true, and the
 * level entered by leave() contains only one item, automatically
 * goes up further until a level with more than one item is reached.
 * \return returns false if there is no further upper level
 */
        bool leave ();

/*! \brief leave the current level, go up in the tree until
 * target level is reached.
 * If fall_through (see constructor) is set to true, and the
 * level entered by leave() contains only one item, automatically
 * goes up further until a level with more than one item is reached.
 * \return returns false if there is no further upper level
 */
        bool leave (const unsigned int target_level)
	{
		while (m_level>target_level)
			if (!leave()) return false;
		return true;
	}

//! \brief the current level in the tree
        unsigned int level () const
        {
            return m_level;
        }

/*! \brief the possible choices for a keyfield in this level.
 * keyfields already used in upper levels are no possible
 * choices, neither are most keyfields if their usage would
 * allow less than 2 choices.
 */
        const strvector &keychoice (const unsigned int level);

/*! \brief returns the current item from the value() list
 */
	string getCurrentValue();

	//! \brief true if the selection holds no items
	bool empty();

/*! \brief returns detailled info about all selected tracks.
 * The ordering is done only by the keyfield of the current level.
 * This might have to be changed - suborder by keyfields of detail
 * levels. This list is cached so several consequent calls mean no
 * loss of performance. See value(), the same warning applies.
 * \todo call this more seldom. See getNumTracks()
 */
        const vector < mgContentItem > &tracks ();

/*! \brief returns an item from the tracks() list
 * \param position the position in the tracks() list
 * \return returns NULL if position is out of range
 */
        mgContentItem* getTrack (unsigned int position);

/*! \brief returns the current item from the tracks() list
 */
        mgContentItem* getCurrentTrack ()
        {
            return getTrack (gotoTrackPosition());
        }

/*! \brief toggles the shuffle mode thru all possible values.
 * When a shuffle modus SM_NORMAL or SM_PARTY is selected, the
 * order of the tracks in the track list will be randomly changed.
 */
        ShuffleMode toggleShuffleMode ();

//! \brief toggles the loop mode thru all possible values
        LoopMode toggleLoopMode ();

//! \brief returns the current shuffle mode
        ShuffleMode getShuffleMode () const
        {
            return m_shuffle_mode;
        }

//! \brief sets the current shuffle mode
        void setShuffleMode (const ShuffleMode shuffle_mode)
        {
            m_shuffle_mode = shuffle_mode;
        }

//! \brief returns the current loop mode
        LoopMode getLoopMode () const
        {
            return m_loop_mode;
        }

//! \brief sets the current loop mode
        void setLoopMode (const LoopMode loop_mode)
        {
            m_loop_mode = loop_mode;
        }

/*! \brief adds the whole current track list to a collection
 * \param Name the name of the collection. If it does not yet exist,
 * it will be created.
 */
        unsigned int AddToCollection (const string Name);

/*! \brief removes the whole current track from a the collection
 * Remember - this selection can be configured to hold exactly
 * one list, so this command can be used to clear a selected list.
 * \param Name the name of the collection
 */
        unsigned int RemoveFromCollection (const string Name);
//! \brief delete a collection
        bool DeleteCollection (const string Name);
/*! \brief create a collection only if it does not yet exist.
 * \return true only if it has been created. false if it already existed.
 */
        bool CreateCollection(const string Name);

//! \brief remove all items from the collection
        void ClearCollection (const string Name);

/*! generates an m3u file containing all tracks. The directory
 * can be indicated by SetDirectory().
 * The file name will be built from the list name, slashes
 * and spaces converted
 */
        string exportM3U ();

/*! \brief go to a position in the current level. If we are at the
 * most detailled level this also sets the track position since
 * they are identical.
 * \param position the wanted position. If it is too big, go to the 
 * last existing position
 * \return only if no position exists, false will be returned
 */
        void setPosition (unsigned int position);

/*! \brief go to the position with value in the current level
 * \param value the value of the wanted position
 */
        void setPosition (const string value)
        {
            setPosition (valindex (value));
        }

/*! \brief go to a position in the track list
 * \param position the wanted position. If it is too big, go to the 
 * last existing position
 * \return only if no position exists, false will be returned
 */
        void setTrack (unsigned int position);

/*! \brief skip some tracks in the track list
 * \return false if new position does not exist
 */
        bool skipTracks (int step=1);

/*! \brief skip forward by 1 in the track list
 * \return false if new position does not exist
 */
        bool skipFwd ()
        {
            return skipTracks (+1);
        }

/*! \brief skip back by 1 in the track list
 * \return false if new position does not exist
 */
        bool skipBack ()
        {
            return skipTracks (-1);
        }

//! \brief returns the sum of the durations of all tracks
        unsigned long getLength ();

/*! \brief returns the sum of the durations of completed tracks
 * those are tracks before the current track position
 */
        unsigned long getCompletedLength ();

/*! returns the number of tracks in the track list
 *  \todo should not call tracks () which loads all track info.
 *  instead, only count the tracks. If the size differs from
 *  m_tracks.size(), invalidate m_tracks
 */
        unsigned int getNumTracks ()
        {
            return tracks ().size ();
        }

//! sets the directory for the storage of m3u file
        void SetDirectory (const string directory)
        {
            m_Directory = directory;
        }

/*! returns the name of the current play list. If no play list is active,
 * the name is built from the name of the key fields.
 */
        string getListname ();

/*! \brief true if this selection currently selects a list of collections
 */
        bool isCollectionlist ();

	//! \brief true if we have entered a collection
	bool inCollection(const string Name="");

	/*! \brief dumps the entire state of this selection into a map,
	 * \param nv the values will be entered into this map
	 */
        void DumpState(mgValmap& nv);

        /*! \brief creates a new selection using saved definitions
	 * \param nv this map contains the saved definitions
	 */
	mgSelection(mgValmap&  nv);

	//! \brief clear the cache, next access will reload from data base
        void clearCache();

	//! \todo soll sql_values() nur noch bei Bedarf bauen, also muessen
	// alle Aenderungen, die Einfluss darauf haben, clearCache machen
        void refreshValues();

	//! \brief true if values and tracks need to be reloaded
	bool cacheIsEmpty()
	{
		return (m_current_values=="" && m_current_tracks=="");
	}
    private:
	void AddOrder(const string sql,list<string>& orderlist, const string item);
    	list < string > m_fromtables; //!< \brief part result from previous where()
    	string m_from;	//!< \brief part result from previous where()
	string m_where;	//!< \brief part result from previous where()
        bool m_fall_through;
        vector < unsigned int >m_position;
        unsigned int m_tracks_position;
        ShuffleMode m_shuffle_mode;
        LoopMode m_loop_mode;
        MYSQL *m_db;
        string m_Host;
        string m_User;
        string m_Password;
	string m_ToplevelDir;
        unsigned int m_level;
        long m_trackid;
        string m_current_values;
        string m_current_tracks;

//! \brief be careful when accessing this, see mgSelection::tracks()
        vector < mgContentItem > m_tracks;
        strvector m_ids;
        strvector m_keychoice;
        artistkeyfield kartist;
        ratingkeyfield krating;
        yearkeyfield kyear;
        decadekeyfield kdecade;
        albumkeyfield kalbum;
        collectionkeyfield kcollection;
        collectionitemkeyfield kcollectionitem;
        genre1keyfield kgenre1;
        genre2keyfield kgenre2;
        langkeyfield klanguage;
        titlekeyfield ktitle;
        trackkeyfield ktrack;
        map < string, keyfield * >all_keys;
        map < string, keyfield * >trall_keys;
        vector < keyfield * >keys;
        bool UsedBefore (keyfield const *k, unsigned int level);
        void InitSelection ();
        void InitDatabase ();
        void initkey (keyfield & f);
	/*! \brief returns the SQL command for getting all values. 
	 * For the leaf level, all values are returned. For upper
	 * levels, every distinct value is returned only once.
	 * This must be so for the leaf level because otherwise
	 * the value() entries do not correspond to the track()
	 * entries and the wrong tracks might be played.
	 */
        string sql_values ();
	//! \todo das nach mgSelStrings verlagern
        unsigned int valindex (const string val,const bool second_try=false);
        string ListFilename ();
        string m_Directory;
        void loadgenres ();
        MYSQL_RES *exec_sql (string query);
        string get_col0 (string query);

	void InitFrom(const mgSelection* s);

/*! \brief executes a query and returns the integer value from
 * the first column in the first row. The query shold be a COUNT query
 * returning only one row.
 * \param query the SQL query to be executed
 */
        unsigned long mgSelection::exec_count (string query);

        keyfield* findKey (const string name);
	map < string, unsigned int > keycounts;
};

//! \brief streams debug info about a selection
ostream & operator<< (ostream &, mgSelection & s);

//! \brief convert the shuffle mode into a string
// \return strings "SM_NONE" etc.
string toString (mgSelection::ShuffleMode);

//! \brief same as toString but returns a C string
const char *toCString (mgSelection::ShuffleMode);

//string toString(long int l);

string itos (int i);
unsigned int randrange (const unsigned int high);


#endif                                            // _DB_H