~ubuntu-branches/ubuntu/trusty/soprano/trusty

« back to all changes in this revision

Viewing changes to client/localsocketclient.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg
  • Date: 2013-01-02 13:02:08 UTC
  • mfrom: (1.1.43)
  • Revision ID: package-import@ubuntu.com-20130102130208-25etkuo00kh8cb5w
Tags: 2.9.0+dfsg.1-0ubuntu1
* New upstream release
  - Update symbols/install files
* Make DFSG compliant

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * This file is part of Soprano Project.
3
3
 *
4
4
 * Copyright (C) 2007-2012 Sebastian Trueg <trueg@kde.org>
 
5
 * Copyright (C) 2012      Vishesh Handa <me@vhanda.in>
5
6
 *
6
7
 * This library is free software; you can redistribute it and/or
7
8
 * modify it under the terms of the GNU Library General Public
34
35
        class LocalSocketClientConnection : public ClientConnection
35
36
        {
36
37
        public:
37
 
            LocalSocketClientConnection( const QString& path, QObject* parent );
 
38
            LocalSocketClientConnection( QObject* parent = 0 );
38
39
            ~LocalSocketClientConnection();
39
40
 
 
41
            void setSocketPath( const QString& path ) {
 
42
                m_socketPath = path;
 
43
            }
 
44
 
 
45
            virtual bool connect();
 
46
            virtual bool disconnect();
 
47
 
 
48
            virtual bool isConnected() {
 
49
                return m_localSocket.isConnected();
 
50
            }
40
51
        protected:
41
 
            Socket *newConnection();
 
52
            virtual Socket* getSocket() {
 
53
                return &m_localSocket;
 
54
            }
42
55
 
43
56
        private:
44
57
            QString m_socketPath;
 
58
            LocalSocket m_localSocket;
45
59
        };
46
60
 
47
 
        LocalSocketClientConnection::LocalSocketClientConnection( const QString& path, QObject* parent )
48
 
            : ClientConnection( parent ),
49
 
              m_socketPath( path )
 
61
        LocalSocketClientConnection::LocalSocketClientConnection( QObject* parent )
 
62
            : ClientConnection( parent )
50
63
        {
51
64
        }
52
65
 
54
67
        {
55
68
        }
56
69
 
57
 
        Socket* LocalSocketClientConnection::newConnection()
58
 
        {
59
 
            clearError();
60
 
            QString path( m_socketPath );
61
 
            if ( path.isEmpty() ) {
62
 
                path = QDir::homePath() + QLatin1String( "/.soprano/socket" );
63
 
            }
64
 
 
65
 
            LocalSocket* socket = new LocalSocket;
66
 
            if ( socket->open( path ) ) {
67
 
                return socket;
68
 
            }
69
 
            else {
70
 
                setError( socket->lastError() );
71
 
                delete socket;
72
 
                return 0;
73
 
            }
 
70
        bool LocalSocketClientConnection::connect()
 
71
        {
 
72
            if( m_localSocket.isConnected() ) {
 
73
                setError( "Already connected" );
 
74
                return false;
 
75
            }
 
76
 
 
77
            if( m_socketPath.isEmpty() ) {
 
78
                m_socketPath = QDir::homePath() + QLatin1String( "/.soprano/socket" );
 
79
            }
 
80
 
 
81
            if ( !m_localSocket.open( m_socketPath ) ) {
 
82
                setError( m_localSocket.lastError() );
 
83
                return false;
 
84
            }
 
85
 
 
86
            return true;
 
87
        }
 
88
 
 
89
        bool LocalSocketClientConnection::disconnect()
 
90
        {
 
91
            if( m_localSocket.isConnected() ) {
 
92
                m_localSocket.close();
 
93
                return true;
 
94
            }
 
95
            return false;
74
96
        }
75
97
    }
76
98
}
83
105
        : connection( 0 ) {
84
106
    }
85
107
 
86
 
    LocalSocketClientConnection* connection;
 
108
    LocalSocketClientConnection connection;
87
109
};
88
110
 
89
111
Soprano::Client::LocalSocketClient::LocalSocketClient( QObject* parent )
103
125
bool Soprano::Client::LocalSocketClient::connect( const QString& name )
104
126
{
105
127
    if ( !isConnected() ) {
106
 
        if ( !d->connection )
107
 
            d->connection = new LocalSocketClientConnection( name, this );
108
 
        if ( d->connection->connect() &&
109
 
             d->connection->checkProtocolVersion() ) {
 
128
        d->connection.setSocketPath( name );
 
129
        if ( d->connection.connect() &&
 
130
             d->connection.checkProtocolVersion() ) {
110
131
            return true;
111
132
        }
112
133
        else {
113
 
            disconnect();
 
134
            setError( d->connection.lastError() );
114
135
            return false;
115
136
        }
116
137
    }
123
144
 
124
145
bool Soprano::Client::LocalSocketClient::isConnected() const
125
146
{
126
 
    return d->connection ? d->connection->isConnected() : false;
 
147
    return d->connection.isConnected();
127
148
}
128
149
 
129
150
 
130
151
void Soprano::Client::LocalSocketClient::disconnect()
131
152
{
132
 
    if (d->connection) {
133
 
        d->connection->deleteLater();
134
 
        d->connection = 0;
135
 
    }
 
153
    d->connection.disconnect();
136
154
}
137
155
 
138
156
 
139
157
Soprano::Model* Soprano::Client::LocalSocketClient::createModel( const QString& name, const QList<BackendSetting>& settings )
140
158
{
141
 
    if ( d->connection ) {
142
 
        int modelId = d->connection->createModel( name, settings );
143
 
        setError( d->connection->lastError() );
 
159
    if ( d->connection.isConnected() ) {
 
160
        int modelId = d->connection.createModel( name, settings );
 
161
        setError( d->connection.lastError() );
144
162
        if ( modelId > 0 ) {
145
 
            StorageModel* model = new ClientModel( 0, modelId, d->connection );
 
163
            StorageModel* model = new ClientModel( 0, modelId, &d->connection );
146
164
            return model;
147
165
        }
148
166
    }
156
174
 
157
175
void Soprano::Client::LocalSocketClient::removeModel( const QString& name )
158
176
{
159
 
    if ( d->connection ) {
160
 
        d->connection->removeModel( name );
161
 
        setError( d->connection->lastError() );
 
177
    if ( d->connection.isConnected() ) {
 
178
        d->connection.removeModel( name );
 
179
        setError( d->connection.lastError() );
162
180
    }
163
181
    else {
164
182
        setError( "Not connected" );