aboutsummaryrefslogtreecommitdiff
path: root/mservicemapper/mservicemapper_p.cpp
blob: 11e72824ce5148593a1bad707a48433a2ae4ce45 (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
/***************************************************************************
**
** 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 <QStringList>
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QIODevice>
#include <QString>
#include <QStringList>
#include <QSettings>

#include "mservicemapper_p.h"

MServiceMapperPrivate::MServiceMapperPrivate( const QString &serviceFileDir ):
    m_serviceFileDir( serviceFileDir )
{
}

void MServiceMapperPrivate::init()
{
    m_serviceFileList.append(QStringList());
    m_serviceFileList.append(QStringList());

    if ( !m_serviceFileDir.isEmpty() ) {
        m_watcher.addPath(m_serviceFileDir);
    }
}

QStringList MServiceMapperPrivate::fillServiceFileList() const
{
    QDir dir = QDir(m_serviceFileDir);
    dir.setNameFilters(QStringList("*.service"));
    return dir.entryList(QDir::Files | QDir::NoSymLinks, QDir::Name);
}

bool MServiceMapperPrivate::fileExistsAndReadable(const QString &fileName) const
{
    bool fileExists = QFile::exists(fileName);
    bool fileIsReadable = (QFile::permissions(fileName) & QFile::ReadUser);

    return fileExists && fileIsReadable;
}

QIODevice *MServiceMapperPrivate::accessFile(const QString &fileName) const
{
    return new QFile(fileName);
}

QString MServiceMapperPrivate::preferredService(const QStringList &serviceList) const
{
    // policy needed, e.g. define policy in repository
    QSettings policy("services.policy", QSettings::IniFormat);
    //policy.setValue("provider", "nokia");
    QString provider = policy.value("provider").toString();
    int serviceIndex = 0;

    if (!provider.isEmpty()) {
        //iterate list to find the 1st service of the provider
        const int size = serviceList.size();
        for (int i = 0; i < size; ++i) {
            if (serviceList[i].contains(provider)) {
                serviceIndex = i;
                break;
            }
        }
    }

    return serviceList[serviceIndex];
}

MServiceMapperPrivate::ServiceInfo MServiceMapperPrivate::serviceInterfacePair(const QString &fileName) const
{
    MServiceMapperPrivate::ServiceInfo serviceInterfacePair;

    if ( m_serviceFileInfo.contains( fileName ) ) {
        serviceInterfacePair = m_serviceFileInfo.value( fileName );
    } else {
        QString absoluteFileName = QDir(m_serviceFileDir).absoluteFilePath(fileName);

        if (fileExistsAndReadable(absoluteFileName)) {
            QIODevice *file = accessFile(absoluteFileName);
            if (file->open(QIODevice::ReadOnly)) {
                QTextStream in(file);

                while (!in.atEnd()) {
                    QString line = in.readLine();

                    QStringList fields = line.split('=');

                    if (fields[0] == "Name") {
                        serviceInterfacePair.service=fields[1];
                    } else
                        if (fields[0] == "Interface") {
                            serviceInterfacePair.interface=fields[1];
                        }
                }
            }

            delete file;
        }
    }

    return serviceInterfacePair;
}

void MServiceMapperPrivate::fillServiceFileInfo()
{
    m_serviceFileList[CurrList] = fillServiceFileList();
    int serviceFileNum = m_serviceFileList[CurrList].size();

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

        m_serviceFileInfo.insert(m_serviceFileList[CurrList][i], serviceInfo);
    }
}