summaryrefslogtreecommitdiffstats
path: root/amarok/src/mediadevice/daap/daapserver.cpp
blob: af0fe7f99aef81203938e1272f10f74ea47bf5cc (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
/***************************************************************************
 * copyright            : (C) 2006 Ian Monroe <ian@monroe.nu>              *
 **************************************************************************/

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

#include "amarok.h"
#include "debug.h"
#include "daapserver.h"
#include "collectiondb.h"

#include <kstandarddirs.h>
#include <kuser.h>
#if DNSSD_SUPPORT
    #include <dnssd/publicservice.h>
#endif
DaapServer::DaapServer(TQObject* parent, char* name)
  : TQObject( parent, name )
  , m_service( 0 )
{
    DEBUG_BLOCK

    m_server = new KProcIO();
    m_server->setComm( KProcess::All );
    *m_server << "amarok_daapserver.rb";
    *m_server << locate( "data", "amarok/ruby_lib/" );
    *m_server << locate( "lib", "ruby_lib/" );
    *m_server << locate( "data", "amarok/scripts/ruby_debug/debug.rb" );
    if( !m_server->start( KProcIO::NotifyOnExit, true ) ) {
        error() << "Failed to start amarok_daapserver.rb" << endl;
        return;
    }

    connect( m_server, TQT_SIGNAL( readReady( KProcIO* ) ), this, TQT_SLOT( readSql() ) );
}

DaapServer::~DaapServer()
{
    #if DNSSD_SUPPORT
        delete m_service;
    #endif
    delete m_server;
}

void
DaapServer::readSql()
{
    static const TQCString sqlPrefix = "SQL TQUERY: ";
    static const TQCString serverStartPrefix = "SERVER STARTING: ";
    TQString line;
    while( m_server->readln( line ) != -1 )
    {
        if( line.startsWith( sqlPrefix ) )
        {
            line.remove( 0, sqlPrefix.length() );
            debug() << "sql run " << line << endl;
            m_server->writeStdin( CollectionDB::instance()->query( line ).join("\n") );
            m_server->writeStdin( TQString("**** END SQL ****") );
        }
        else if( line.startsWith( serverStartPrefix ) )
        {
            line.remove( 0, serverStartPrefix.length() );
            debug() << "Server starting on port " << line << '.' << endl;
            #if DNSSD_SUPPORT
                KUser current;
                if( !m_service )
                    m_service = new DNSSD::PublicService( i18n("%1's Amarok Share").tqarg( current.fullName() ), "_daap._tcp", line.toInt() );
                    debug() << "port number: " << line.toInt() << endl;
                m_service->publishAsync();
            #endif
        }
        else
            debug() << "server says " << line << endl;
   }
   //m_server->ackRead();
   //m_server->enableReadSignals(true);
}

#include "daapserver.moc"