summaryrefslogtreecommitdiffstats
path: root/amarok/src/device/smb/smbdevicehandler.cpp
blob: 072c36a82ceecf912bc28a456e954a9e73f3929b (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
/*
 *  Copyright (c) 2006-2007 Maximilian Kossick <maximilian.kossick@googlemail.com>
 *
 *  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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include "smbdevicehandler.h"

AMAROK_EXPORT_PLUGIN( SmbDeviceHandlerFactory )

#include "debug.h"

#include <kconfig.h>
#include <kurl.h>

#include <tqvaluelist.h>


SmbDeviceHandler::SmbDeviceHandler( int deviceId, TQString server, TQString dir, TQString mountPoint )
    : DeviceHandler()
    , m_deviceID( deviceId )
    , m_mountPoint( mountPoint )
    , m_server( server )
    , m_dir( dir )
{
}

SmbDeviceHandler::~SmbDeviceHandler()
{
}

bool
SmbDeviceHandler::isAvailable() const
{
    return true;
}


TQString
SmbDeviceHandler::type() const
{
    return "smb";
}

int
SmbDeviceHandler::getDeviceID()
{
    return m_deviceID;
}

const TQString &
SmbDeviceHandler::getDevicePath() const
{
    return m_mountPoint;
}

void
SmbDeviceHandler::getURL( KURL &absolutePath, const KURL &relativePath )
{
    absolutePath.setPath( m_mountPoint );
    absolutePath.addPath( relativePath.path() );
    absolutePath.cleanPath();
}

void
SmbDeviceHandler::getPlayableURL( KURL &absolutePath, const KURL &relativePath )
{
    getURL( absolutePath, relativePath );
}

bool
SmbDeviceHandler::deviceIsMedium( const Medium * m ) const
{
    return m->deviceNode() == m_server + ':' + m_dir;
}

///////////////////////////////////////////////////////////////////////////////
// class SmbDeviceHandlerFactory 
///////////////////////////////////////////////////////////////////////////////

TQString
SmbDeviceHandlerFactory::type( ) const
{
    return "smb";
}

bool
SmbDeviceHandlerFactory::canCreateFromMedium( ) const
{
    return true;
}

bool
SmbDeviceHandlerFactory::canCreateFromConfig( ) const
{
    return false;
}

bool
SmbDeviceHandlerFactory::canHandle( const Medium * m ) const
{
    return m && ( m->fsType().find( "smb" ) != -1 ||
                  m->fsType().find( "cifs" ) != -1 ) 
		&& m->isMounted();
}

SmbDeviceHandlerFactory::SmbDeviceHandlerFactory( )
{
}

SmbDeviceHandlerFactory::~SmbDeviceHandlerFactory( )
{
}

DeviceHandler *
SmbDeviceHandlerFactory::createHandler( const KConfig* ) const
{
    return 0;
}

DeviceHandler *
SmbDeviceHandlerFactory::createHandler( const Medium * m ) const
{
    TQString server = m->deviceNode().section( "/", 2, 2 );
    TQString share = m->deviceNode().section( "/", 3, 3 );
    TQStringList ids = CollectionDB::instance()->query( TQString( "SELECT id, label, lastmountpoint "
                                                                "FROM devices WHERE type = 'smb' "
                                                                "AND servername = '%1' AND sharename = '%2';" )
                                                                .tqarg( server )
                                                                .tqarg( share ) );
    if ( ids.size() == 3 )
    {
        debug() << "Found existing SMB config for ID " << ids[0] << " , server " << server << " ,share " << share << endl;
        CollectionDB::instance()->query( TQString( "UPDATE devices SET lastmountpoint = '%2' WHERE "
                                                  "id = %1;" ).tqarg( ids[0] ).tqarg( m->mountPoint() ) );
        return new SmbDeviceHandler( ids[0].toInt(), server, share, m->mountPoint() );
    }
    else
    {
        int id = CollectionDB::instance()->insert( TQString( "INSERT INTO devices"
                                                            "( type, servername, sharename, lastmountpoint ) "
                                                            "VALUES ( 'smb', '%1', '%2', '%3' );" )
                                                            .tqarg( server )
                                                            .tqarg( share )
                                                            .tqarg( m->mountPoint() ), "devices" );
        if ( id == 0 )
        {
            warning() << "Inserting into devices failed for type=smb, server=" << server << ", share=" << share << endl;
            return 0;
        }
        debug() << "Created new SMB device with ID " << id << " , server " << server << " ,share " << share << endl;
        return new SmbDeviceHandler( id, server, share, m->mountPoint() );
    }
}