summaryrefslogtreecommitdiffstats
path: root/amarok/src/mediadevice/daap/daapclient.h
blob: c71403968d557e1861155579960f88a96f263b7c (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
/***************************************************************************
 * copyright            : (C) 2006 Ian Monroe <ian@monroe.nu>              *
 *                        (C) 2006 Seb Ruiz <me@sebruiz.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 AMAROK_DAAPCLIENT_H
#define AMAROK_DAAPCLIENT_H

#include "daapreader/reader.h"
#include "mediabrowser.h"
#include "threadmanager.h"

#include <tdeversion.h>
#include <kdialogbase.h>
#include <ktempfile.h>

#define DNSSD_SUPPORT KDE_IS_VERSION(3,4,0)

#if DNSSD_SUPPORT
    #include <dnssd/remoteservice.h> //for DNSSD::RemoteService::Ptr
#else
namespace DNSSD {
    namespace RemoteService {
        class Ptr {}; //HACK Dummy class, so that daapclient.moc compiles
    }
}
#endif

namespace DNSSD {
    class ServiceBrowser;
}

class AddHostBase;
class MediaItem;
class ServerItem;
class DaapServer;

class KURL;

class TQCheckBox;
class TQString;
class TQTimer;

class DaapClient : public MediaDevice
{
    Q_OBJECT
  
   public:
        struct ServerInfo
        {
            ServerInfo() : sessionId( -1 ), revisionID( 10 ) { }
            int sessionId;
            int revisionID;
        };

        DaapClient();
        virtual ~DaapClient();
        bool isConnected();

        virtual bool needsManualConfig() { return true; }
        virtual void addConfigElements   ( TQWidget * /*parent*/ );
        virtual void removeConfigElements( TQWidget * /*parent*/ );
        virtual void applyConfig();
        virtual void loadConfig();


        int incRevision( const TQString& host );
        int getSession( const TQString& host );
        KURL getProxyUrl( const KURL& url );
        void customClicked();
        bool autoConnect() { return true; }

    public slots:
         void passwordPrompt();

    protected:
         bool getCapacity( TDEIO::filesize_t *total, TDEIO::filesize_t *available );
         void rmbPressed( TQListViewItem* qitem, const TQPoint& point, int );
         bool lockDevice( bool tryOnly = false );
         void unlockDevice();
         bool openDevice( bool silent=false );
         bool closeDevice();
         void synchronizeDevice();
         MediaItem* copyTrackToDevice(const MetaBundle& bundle);
         MediaItem* trackExists( const MetaBundle& );
         virtual int deleteItemFromDevice( MediaItem *item, int flags=DeleteTrack );

   private slots:
        void serverOffline( DNSSD::RemoteService::Ptr );
        void foundDaap( DNSSD::RemoteService::Ptr );
        void resolvedDaap( bool );
        void createTree( const TQString& host, Daap::SongList bundles );
        void broadcastButtonToggled();

   private:
        ServerItem* newHost( const TQString& serviceName, const TQString& host, const TQString& ip, const TQ_INT16 port );
        void downloadSongs( KURL::List urls );
        TQString resolve( const TQString& hostname );
#if DNSSD_SUPPORT
        TQString serverKey( const DNSSD::RemoteService* service ) const;
        DNSSD::ServiceBrowser* m_browser;
#endif
        /// @return true if track is already in the local collection
        const bool trackExistsInCollection( MetaBundle *bundle );

        bool    m_connected;
        TQMap<TQString, ServerInfo*> m_servers;
        TQMap<TQString, ServerItem*> m_serverItemMap;

        DaapServer *m_sharingServer;
        TQCheckBox  *m_broadcastServerCheckBox;
        TQCheckBox  *m_removeDuplicatesCheckBox;
        bool        m_broadcastServer;
        bool        m_removeDuplicates;
        KToolBarButton *m_broadcastButton;

        // if set to true, we don't display items that the user already has in the collection
        bool        m_hideMusicOwned;
};

class ServerItem : public TQObject, public MediaItem
{
    Q_OBJECT
  

    public:
        ServerItem( TQListView* parent, DaapClient* client, const TQString& ip, TQ_UINT16 port, const TQString& title, const TQString& host );
        ~ServerItem();
        void setOpen( bool o );
        void resetTitle()                     { setText( 0, m_title ); }
        void unLoaded()                       { m_loaded = false; }
        void setReader( Daap::Reader* reader) { m_reader = reader; }
        Daap::Reader* getReader() const       { return m_reader; }

        void startAnimation();
        void stopAnimation();

        TQString key() const { return key( m_host, m_port ); }
        void reset();
        static TQString key( const TQString& host, TQ_UINT16 port ) { return host + ':' + TQString::number( port ); }
    public slots:
        void httpError( const TQString& );
    private slots:
        void slotAnimation();

    private:
        DaapClient     *m_daapClient;
        Daap::Reader   *m_reader;
        const TQString   m_ip;
        const TQ_UINT16  m_port;
        const TQString   m_title;
        const TQString   m_host;
        bool            m_loaded;

        TQPixmap        *m_loading1, *m_loading2;    //icons for loading animation
        TQTimer          m_animationTimer;
        uint            m_iconCounter;

        static const int ANIMATION_INTERVAL = 250;
};

#ifdef Q_MOC_RUN
// MOC_SKIP_BEGIN
class DaapDownloader : public JobBase
// MOC_SKIP_END
#else
class DaapDownloader : public ThreadManager::Job
#endif
{
Q_OBJECT
  
public:
    DaapDownloader( KURL::List urls );

    virtual bool doJob();

    virtual void completeJob();

private slots:
    void downloadFinished( int id, bool error );
    void dataReadProgress( int done, int total );
    void downloadFailed( const TQString &error );

private:
    KURL::List m_urls;
    TQValueList<KTempFile*> m_tempFileList;
    bool m_ready;
    bool m_successful;
    bool m_errorOccured;
};

#endif /*AMAROK_DAAPCLIENT_H*/