aboutsummaryrefslogtreecommitdiff
path: root/mservicemapper/mservicemapper.cpp
blob: 0c4a6073292d6c846fba01d2f2d5bc99d70e7a7a (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
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "mservicemapper.h"

#include <QDir>
#include <QSettings>
#include <QDebug>

#include "mservicemapper_p.h"

#ifdef HAVE_QTM_SERVICEMAPPER
#include <QServiceManager>

QTM_USE_NAMESPACE
#endif

MServiceMapper::MServiceMapper(const QString &serviceFileDir) :
    d_ptr(new MServiceMapperPrivate( serviceFileDir ))
{
    init();
}

void MServiceMapper::init()
{
    d_ptr->init();
    connect(&d_ptr->m_watcher, SIGNAL(directoryChanged(QString)),
            this, SLOT(handleServiceChanged(QString)));

    handleServiceChanged( QString() );
}

MServiceMapper::~MServiceMapper()
{
    delete d_ptr;
}

QString MServiceMapper::serviceName(const QString &interfaceName)
{
    QStringList serviceNameList = serviceNames(interfaceName);

    QString serviceName;
    if (!serviceNameList.isEmpty()) {
        serviceName = d_ptr->preferredService(serviceNameList);
    }

    bool serviceNameInvalid = serviceName.contains(" ");   // "not provided" - when the service wouldn't run
    if (serviceNameInvalid) {
        serviceName.clear();
    }

    return serviceName;
}

QStringList MServiceMapper::serviceNames(const QString &interfaceName)
{
    d_ptr->m_serviceFileList[CurrList] = d_ptr->fillServiceFileList();
    int serviceFileNum = d_ptr->m_serviceFileList[CurrList].size();

    QStringList serviceNameList;

    for (int i = 0; i < serviceFileNum; ++i) {
        MServiceMapperPrivate::ServiceInfo serviceInfo = d_ptr->serviceInterfacePair(d_ptr->m_serviceFileList[CurrList][i]);

        if (interfaceName.isEmpty() || serviceInfo.interface == interfaceName) {
            serviceNameList << serviceInfo.service;
            d_ptr->m_serviceFileInfo.insert(d_ptr->m_serviceFileList[CurrList][i], serviceInfo);
        }
    }

    return serviceNameList;
}

void MServiceMapper::handleServiceChanged(const QString &path)
{
    Q_UNUSED(path);   // we do not support changing of the service dir
    // on the fly for now.

    d_ptr->m_serviceFileList[LastList] = d_ptr->m_serviceFileList[CurrList];

    d_ptr->m_serviceFileList[CurrList] = d_ptr->fillServiceFileList();

    // find removed files - those in LastList that aren't in CurrList
    int lastListSize = d_ptr->m_serviceFileList[LastList].size();
    for ( int index=0; index<lastListSize; ++index ) {
        QString thisFile = d_ptr->m_serviceFileList[LastList][index];
        bool fileRemoved = !d_ptr->m_serviceFileList[CurrList].contains( thisFile );
        if ( fileRemoved ) {
            QString thisServiceName = d_ptr->m_serviceFileInfo.take(thisFile).service;

            qDebug() << "removing" << thisServiceName;

#ifdef HAVE_QTM_SERVICEMAPPER
            QServiceManager serviceManager;
            serviceManager.removeService( thisServiceName );
#endif
            emit serviceUnavailable( thisServiceName );
        }
    }

    // get info for new services
    d_ptr->fillServiceFileInfo();

    // find added files - those in CurrList that aren't in LastList
    int currListSize = d_ptr->m_serviceFileList[CurrList].size();
    for ( int index=0; index<currListSize; ++index ) {
        QString thisFile = d_ptr->m_serviceFileList[CurrList][index];
        bool fileAdded = !d_ptr->m_serviceFileList[LastList].contains( thisFile );
        if ( fileAdded ) {
            MServiceMapperPrivate::ServiceInfo thisServiceInterfacePair =
                d_ptr->serviceInterfacePair( thisFile );
            QString thisService   = thisServiceInterfacePair.service;
            QString thisInterface = thisServiceInterfacePair.interface;

            QString xmlFileName( "/usr/lib/maemo-meegotouch-services/"+thisService+".xml" );
            qDebug() << "adding" << xmlFileName;

#ifdef HAVE_QTM_SERVICEMAPPER
            QServiceManager serviceManager;
            serviceManager.addService( xmlFileName );
#endif

            emit serviceAvailable( thisService, thisInterface );
        }
    }

}

QString MServiceMapper::servicePath(const QString &interfaceName) const
{
    Q_UNUSED(interfaceName);
    // Iterate through installed services and return the preferred one
    // for now all services' object paths are "/"
    return QString("/");
}

QString MServiceMapper::interfaceName(const QString &serviceName) const
{
    if (serviceName.isEmpty())
        return QString();

    QString retVal;
    d_ptr->m_serviceFileList[CurrList] = d_ptr->fillServiceFileList();
    int serviceFileNum = d_ptr->m_serviceFileList[CurrList].size();

    QStringList serviceNameList;

    bool serviceFound = false;
    bool atEndOfList = false;
    int i = 0;
    while (!serviceFound && !atEndOfList) {
        MServiceMapperPrivate::ServiceInfo thisServiceInterfacePair = d_ptr->serviceInterfacePair(d_ptr->m_serviceFileList[CurrList][i]);

        if (thisServiceInterfacePair.service == serviceName) {
            retVal = thisServiceInterfacePair.interface;
            serviceFound = true;
        }

        if (i < serviceFileNum-1) {
            i++;
        } else {
            atEndOfList = true;
        }
    }

    return retVal;
}

#ifdef UNIT_TEST
MServiceMapper::MServiceMapper(MServiceMapperPrivate *priv) :
    d_ptr(priv)
{
    init();
}
#endif // UNIT_TEST