1 /****************************************************************************************
2  * Copyright (c) 2004-2005 Max Howell <max.howell@methylblue.com>                       *
3  * Copyright (c) 2009 Mark Kretschmann <kretschmann@kde.org>                            *
4  *                                                                                      *
5  * This program is free software; you can redistribute it and/or modify it under        *
6  * the terms of the GNU General Public License as published by the Free Software        *
7  * Foundation; either version 2 of the License, or (at your option) any later           *
8  * version.                                                                             *
9  *                                                                                      *
10  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
12  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
13  *                                                                                      *
14  * You should have received a copy of the GNU General Public License along with         *
15  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
16  ****************************************************************************************/
17 
18 #ifndef AMAROK_H
19 #define AMAROK_H
20 
21 #include "core/amarokcore_export.h"
22 #include "Version.h"
23 #include "core/meta/forward_declarations.h"
24 
25 #include <KActionCollection>
26 #include <KConfigGroup>
27 
28 #include <QDir>
29 #include <QPointer>
30 
31 class QColor;
32 class QDateTime;
33 class QMutex;
34 class QPixmap;
35 
36 namespace KIO { class Job; }
37 
38 namespace Amarok
39 {
40     const int VOLUME_MAX = 100;
41     const int SCOPE_SIZE = 9; // = 2**9 = 512
42     const int blue       = 0x202050;
43     const int VOLUME_SENSITIVITY = 30; // for mouse wheels
44     const int GUI_THREAD_ID = 0;
45 
46     extern QMutex globalDirsMutex;
47     extern QPointer<KActionCollection> actionCollectionObject;
48 
49     namespace ColorScheme
50     {
51         ///eg. base of the Amarok Player-window
52         extern QColor Base; //Amarok::blue
53         ///eg. text in the Amarok Player-window
54         extern QColor Text; //Qt::white
55         ///eg. background colour for Amarok::PrettySliders
56         extern QColor Background; //brighter blue
57         ///eg. outline of slider widgets in Player-window
58         extern QColor Foreground; //lighter blue
59         ///eg. K3ListView alternative row color
60         extern QColor AltBase; //grey toned base
61     }
62 
63     /** The version of the playlist XML format. Increase whenever it changes backwards-incompatibly. */
xmlVersion()64     inline QString xmlVersion() { return QStringLiteral("2.4"); }
65 
66     /**
67      * Convenience function to return the QApplication instance KConfig object
68      * pre-set to a specific group.
69      * @param group Will pre-set the KConfig object to this group.
70      */
71     /* FIXME: This function can lead to very bizarre and hard to figure bugs.
72               While we don`t fix it properly, use it like this: amarok::config( Group )->readEntry( ... ) */
73     AMAROKCORE_EXPORT KConfigGroup config( const QString &group = QStringLiteral("General") );
74 
75     /**
76      * @return the KActionCollection used by Amarok
77      */
78     AMAROKCORE_EXPORT KActionCollection *actionCollection();
79 
80     /**
81      * Compute score for a track that has finished playing.
82      * The resulting score is between 0 and 100
83      */
computeScore(double oldScore,unsigned int oldPlayCount,double playedFraction)84     inline double computeScore( double oldScore, unsigned int oldPlayCount, double playedFraction )
85     {
86         const int percentage = qBound(0, int(playedFraction * 100), 100);
87         double newScore;
88 
89         if( oldPlayCount <= 0 )
90             newScore = ( oldScore + percentage ) / 2;
91         else
92             newScore = ( ( oldScore * oldPlayCount ) + percentage ) / ( oldPlayCount + 1 );
93 
94         return qBound( 0.0, newScore, 100.0 );
95     }
96 
97     /**
98      * Allocate one on the stack, and it'll set the busy cursor for you until it
99      * is destroyed
100      */
101     class OverrideCursor {
102     public:
103         explicit OverrideCursor( Qt::CursorShape cursor = Qt::WaitCursor );
104        ~OverrideCursor();
105     };
106 
107     /**
108      * For saving files to ~/.local/share/amarok/
109      * @param directory: Subdirectory of ~/.local/share/amarok/ to save files to.
110      */
111     AMAROKCORE_EXPORT QString saveLocation( const QString &directory = QString() );
112 
113     AMAROKCORE_EXPORT QString defaultPlaylistPath();
114 
115     AMAROKCORE_EXPORT QString verboseTimeSince( const QDateTime &datetime );
116     AMAROKCORE_EXPORT QString verboseTimeSince( uint time_t );
117     AMAROKCORE_EXPORT QString conciseTimeSince( uint time_t );
118 
119     /**
120      * @return the LOWERCASE file extension without the preceding '.', or "" if there is none
121      */
extension(const QString & fileName)122     inline QString extension( const QString &fileName )
123     {
124         if( fileName.contains( QLatin1Char('.') ) )
125         {
126             QString ext = fileName.mid( fileName.lastIndexOf( QLatin1Char('.') ) + 1 ).toLower();
127             // Remove url parameters (some remote playlists use these)
128             if( ext.contains( QLatin1Char('?') ) )
129                 return ext.left( ext.indexOf( QLatin1Char('?') ) );
130             return ext;
131         }
132 
133         return QString();
134     }
135 
136     void setUseScores( bool use );
137     void setUseRatings( bool use );
138 
139     bool repeatNone(); // defined in ActionClasses.cpp
140     bool repeatTrack();
141     bool repeatAlbum();
142     bool repeatPlaylist();
143     bool randomOff();
144     bool randomTracks();
145     bool randomAlbums();
146     bool repeatEnabled();
147     bool randomEnabled();
148     bool favorNone();
149     bool favorScores();
150     bool favorRatings();
151     bool favorLastPlay();
152 
153     /**
154      * Removes accents from the string
155      * @param path The original path.
156      * @return The cleaned up path.
157      */
158     AMAROKCORE_EXPORT QString cleanPath( const QString &path );
159 
160     /**
161      * Replaces all non-ASCII characters with '_'.
162      * @param path The original path.
163      * @return The ASCIIfied path.
164      */
165     AMAROKCORE_EXPORT QString asciiPath( const QString &path );
166 
167     /**
168      * Define how Amarok::vfatPath() should behave wrt path separators:
169      *
170      * AutoBehaviour: use WindowsBehaviour when compiled in Windows and UnixBehaviour
171      * on everything else
172      * UnixBehaviour: treat / as path separator and \ as a valid part of file name
173      * WindowsBehaviour: treat \ as path separator and / as a valid part of file name
174      */
175     enum PathSeparatorBehaviour
176     {
177         AutoBehaviour,
178         UnixBehaviour,
179         WindowsBehaviour
180     };
181 
182     /**
183      * Transforms path into one valid on VFAT file systems, leaves QDir::separator()s untouched.
184      * Beware: Truncates path to 255 characters!
185      * Replacement rules: illegal characters are being replaced by '_'
186      *                    reserved device names are being prefixed with '_'
187      *                    for file/folder names or extensions that end with a space it will be replaced by '_'
188      * @param path The original path.
189      * @param behaviour see PathSeparatorBehaviour.
190      * @return The cleaned up path.
191      */
192     AMAROKCORE_EXPORT QString vfatPath( const QString &path,
193                                          PathSeparatorBehaviour behaviour = AutoBehaviour );
194 
195     /* defined in browsers/CollectionTreeItemModel.cpp */
196     /**
197      * Small function aimed to convert Eagles, The -> The Eagles (and back again).
198      * If there is no "the" in the name then the string is not changed.
199      * @param str the string to manipulate
200      * @param reverse if true, The Eagles -> Eagles, The. If false, Eagles, The -> The Eagles
201      */
202     AMAROKCORE_EXPORT void manipulateThe( QString &str, bool reverse );
203 
204     /**
205       * Return a playlist name based on the artist and album info of the tracks or a string
206       * containing the creation date.
207       */
208     AMAROKCORE_EXPORT QString generatePlaylistName( const Meta::TrackList &tracks );
209 
210     /**
211      * Creates a semi-transparent Amarok logo for suitable for painting.
212      * @param dim width of the logo
213      * @return A QPixmap of the logo
214      */
215     AMAROKCORE_EXPORT QPixmap semiTransparentLogo( int dim );
216 
discogsApiKey()217     inline const char* discogsApiKey() { return "91734dd989"; }
lastfmApiKey()218     inline const char* lastfmApiKey() { return "402d3ca8e9bc9d3cf9b85e1202944ca5"; }
lastfmApiSharedSecret()219     inline const char* lastfmApiSharedSecret() { return "fe0dcde9fcd14c2d1d50665b646335e9"; }
flickrApiKey()220     inline const char* flickrApiKey() { return "9c5a288116c34c17ecee37877397fe31"; }
221 }
222 
223 #endif
224