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
6
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation, either version 2 of the License, or
8
    (at your option) any later version.
9
10
    This program is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU General Public License for more details.
14
15
    You should have received a copy of the GNU General Public License
16
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#ifndef AMAROK_H
20
#define AMAROK_H
21
22
#include "amarok_export.h"
23
24
#include <KActionCollection>
25
#include <KConfig>
26
#include <KIO/NetAccess>
27
#include <KUrl> // recursiveUrlExpand
28
29
#include <QPointer>
30
31
class QColor;
32
class QDateTime;
33
class QEvent;
34
class QMutex;
35
class QPixmap;
36
class QWidget;
37
38
namespace KIO { class Job; }
39
40
namespace Amarok
41
{
42
    const int VOLUME_MAX = 100;
43
    const int SCOPE_SIZE = 9; //= 2**9 = 512
44
    const int blue       = 0x202050;
45
    const int VOLUME_SENSITIVITY = 30; //for mouse wheels
46
    const int GUI_THREAD_ID = 0;
47
48
    extern QMutex globalDirsMutex; // defined in App.cpp
49
    extern QPointer<KActionCollection> actionCollectionObject; // defined in App.cpp
50
51
52
    namespace ColorScheme
53
    {
54
        ///eg. base of the Amarok Player-window
55
        extern QColor Base; //Amarok::blue
56
        ///eg. text in the Amarok Player-window
57
        extern QColor Text; //Qt::white
58
        ///eg. background colour for Amarok::PrettySliders
59
        extern QColor Background; //brighter blue
60
        ///eg. outline of slider widgets in Player-window
61
        extern QColor Foreground; //lighter blue
62
        ///eg. K3ListView alternative row color
63
        extern QColor AltBase; //grey toned base
64
    }
65
66
    /** The version of the playlist XML format. Increase whenever it changes backwards-incompatibly. */
67
    inline QString xmlVersion() { return "2.4"; }
68
69
    /**
70
     * Convenience function to return the KApplication instance KConfig object
71
     * pre-set to a specific group.
72
     * @param group Will pre-set the KConfig object to this group.
73
     */
74
    /* FIXME: This function can lead to very bizarre and hard to figure bugs.
75
              While we don`t fix it properly, use it like this: amarok::config( Group )->readEntry( ... ) */
76
    AMAROK_EXPORT KConfigGroup config( const QString &group = "General" ); //defined in App.cpp
77
78
    /**
79
     * @return the KActionCollection used by Amarok
80
     */
81
    KActionCollection *actionCollection(); //defined in App.cpp
82
83
    /**
84
     * Compute score for a track that has finished playing.
85
     */
86
    inline double computeScore( double oldScore, int playCount, double playedFraction )
87
    {
88
        const int percentage = static_cast<int>(playedFraction * 100); 
89
        double newScore;
90
91
        if( playCount == 0 )
92
            newScore = ( oldScore + percentage ) / 2;
93
        else
94
            newScore = ( ( oldScore * playCount ) + percentage ) / ( playCount + 1 );
95
96
        return newScore;
97
    }
98
99
    /**
100
     * The mainWindow is the playlistWindow
101
     */
102
    AMAROK_EXPORT QWidget *mainWindow(); //defined in App.cpp
103
104
    /**
105
     * Allocate one on the stack, and it'll set the busy cursor for you until it
106
     * is destroyed
107
     */
108
    class OverrideCursor { //defined in App.cpp
109
    public:
110
        OverrideCursor( Qt::CursorShape cursor = Qt::WaitCursor );
111
       ~OverrideCursor();
112
    };
113
114
    /**
115
     * For saving files to ~/.kde/share/apps/amarok/directory
116
     * @param directory will be created if not existing, you MUST end the string
117
     *                  with '/'
118
     */
119
    AMAROK_EXPORT QString saveLocation( const QString &directory = QString() ); //defined in collectionreader.cpp
120
121
    KIO::Job *trashFiles( const KUrl::List &files ); //defined in App.cpp
122
123
    /**
124
     * For recursively expanding the contents of a directory into a KUrl::List
125
     * (playlists are ignored)
126
     */
127
128
    //New in Amarok2 -> recursiveUrlExpand has been replaced
129
    //existing code depending on this port need to be changed (max urls is removed)
130
    AMAROK_EXPORT KUrl::List recursiveUrlExpand( const KUrl &url ); //defined in PlaylistHandler.cpp
131
    AMAROK_EXPORT KUrl::List recursiveUrlExpand( const KUrl::List &urls ); //defined in PlaylistHandler.cpp
132
133
    AMAROK_EXPORT QString verboseTimeSince( const QDateTime &datetime ); // defined in App.cpp
134
135
    AMAROK_EXPORT QString verboseTimeSince( uint time_t ); // defined in App.cpp
136
137
    AMAROK_EXPORT QString conciseTimeSince( uint time_t ); // defined in App.cpp
138
139
    /**
140
     * Function that must be used when separating contextBrowser escaped urls
141
     */
142
    // defined in App.cpp
143
    void albumArtistTrackFromUrl( QString url, QString &artist, QString &album, QString &detail );
144
145
    /**
146
     * @return the LOWERCASE file extension without the preceding '.', or "" if there is none
147
     */
148
    inline QString extension( const QString &fileName )
149
    {
150
        if( fileName.contains( '.' ) )
151
        {
152
            QString ext = fileName.mid( fileName.lastIndexOf( '.' ) + 1 ).toLower();
153
            // Remove url parameters (some remote playlists use these)
154
            if( ext.contains( '?' ) )
155
                return ext.left( ext.indexOf( '?' ) );
156
            return ext;
157
        }
158
159
        return QString();
160
    }
161
162
    /** Transform url into a file url if possible */
163
    inline KUrl mostLocalURL( const KUrl &url )
164
    {
165
        return KIO::NetAccess::mostLocalUrl( url, mainWindow() );
166
    }
167
168
    /**
169
     * @return the last directory in @param fileName
170
     */
171
    inline QString directory( const QString &fileName )
172
    {
173
        return fileName.section( '/', 0, -2 );
174
    }
175
176
    /**
177
    * Returns internal code for database type, DbConnection::sqlite or DbConnection::mysql
178
    * @param type either "SQLite", or "MySQL".
179
    */
180
    int databaseTypeCode( const QString type ); //defined in configdialog.cpp
181
182
    void setUseScores( bool use ); //defined in App.cpp
183
    void setUseRatings( bool use );
184
185
    bool repeatNone(); //defined in actionclasses.cpp
186
    bool repeatTrack();
187
    bool repeatAlbum();
188
    bool repeatPlaylist();
189
    bool randomOff();
190
    bool randomTracks();
191
    bool randomAlbums();
192
    bool repeatEnabled();
193
    bool randomEnabled();
194
    bool favorNone();
195
    bool favorScores();
196
    bool favorRatings();
197
    bool favorLastPlay();
198
199
    void setDynamicPlaylist( const QString& title );  // defined in dynamicmodel.cpp
200
    void enableDynamicMode( bool enable );
201
202
203
    QStringList splitPath( QString path ); //defined in playlistbrowser.cpp
204
205
    /**
206
     * Removes accents from the string
207
     * @param path The original path.
208
     * @return The cleaned up path.
209
     */
210
    AMAROK_EXPORT QString cleanPath( const QString &path ); //defined in App.cpp
211
212
    /**
213
     * Replaces all non-ASCII characters with '_'.
214
     * @param path The original path.
215
     * @return The ASCIIfied path.
216
     */
217
    AMAROK_EXPORT QString asciiPath( const QString &path ); //defined in App.cpp
218
219
    /**
220
     * Transform path into one valid on VFAT file systems
221
     * @param path The original path.
222
     * @return The cleaned up path.
223
     */
224
    AMAROK_EXPORT QString vfatPath( const QString &path ); //defined in App.cpp
225
226
    /**
227
     * Compare both strings from left to right and remove the common part from input
228
     * @param input the string that get's cleaned.
229
     * @param ref a reference to compare input with.
230
     * @return The cleaned up string.
231
     */
232
    AMAROK_EXPORT QString decapitateString( const QString &input, const QString &ref );
233
234
    /*
235
     * Transform to be usable within HTML/HTML attributes
236
     */
237
    AMAROK_EXPORT QString escapeHTMLAttr( const QString &s ); // defined in App.cpp
238
    AMAROK_EXPORT QString unescapeHTMLAttr( const QString &s ); //defined in App.cpp
239
240
    /* defined in scriptmanager.cpp */
241
    /**
242
     * Returns the proxy that should be used for a given URL.
243
     * @param url the url.
244
     * @return The url of the proxy, or a empty string if no proxy should be used.
245
     */
246
    QString proxyForUrl(const QString& url);
247
248
    /**
249
     * Returns the proxy that should be used for a given protocol.
250
     * @param protocol the protocol.
251
     * @return The url of the proxy, or a empty string if no proxy should be used.
252
     */
253
    QString proxyForProtocol(const QString& protocol);
254
255
    /*defined in collectionbrowser/collectiontreeitemmodel.cpp */
256
    /**
257
     * Small function aimed to convert Eagles, The -> The Eagles (and back again).
258
     * @param str the string to manipulate
259
     * @param reverse if true, The Eagles -> Eagles, The. If false, Eagles, The -> The Eagles
260
     */
261
    AMAROK_EXPORT void manipulateThe( QString &str, bool reverse );
262
263
}
264
265
266
/**
267
 * Use this to const-iterate over QStringLists, if you like.
268
 * Watch out for the definition of last in the scope of your for.
269
 *
270
 *     QStringList strings;
271
 *     oldForeach( strings )
272
 *         debug() << *it << endl;
273
 */
274
#define oldForeach( x ) \
275
    for( QStringList::ConstIterator it = x.constBegin(), end = x.constEnd(); it != end; ++it )
276
277
/**
278
 * You can use this for lists that aren't QStringLists.
279
 * Watch out for the definition of last in the scope of your for.
280
 *
281
 *     BundleList bundles;
282
 *     oldForeachType( BundleList, bundles )
283
 *         debug() << *it.url() << endl;
284
 */
285
#define oldForeachType( Type, x ) \
286
    for( Type::ConstIterator it = x.constBegin(), end = x.constEnd(); it != end; ++it )
287
288
/**
289
 * Creates iterators of type @p Type.
290
 * Watch out for the definitions of last and end in your scope.
291
 *
292
 *     BundleList bundles;
293
 *     for( for_iterators( BundleList, bundles ); it != end; ++it )
294
 *         debug() << *it.url() << endl;
295
 */
296
#define for_iterators( Type, x ) \
297
    Type::ConstIterator it = x.constBegin(), end = x.constEnd(), last = x.fromLast()
298
299
300
/// Update this when necessary
301
#define APP_VERSION "2.2-SVN"
302
303
#endif