34
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (developer.feedback@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the HbServers module of the UI Extensions for Mobile.
|
|
8 |
**
|
|
9 |
** GNU Lesser General Public License Usage
|
|
10 |
** This file may be used under the terms of the GNU Lesser General Public
|
|
11 |
** License version 2.1 as published by the Free Software Foundation and
|
|
12 |
** appearing in the file LICENSE.LGPL included in the packaging of this file.
|
|
13 |
** Please review the following information to ensure the GNU Lesser General
|
|
14 |
** Public License version 2.1 requirements will be met:
|
|
15 |
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
16 |
**
|
|
17 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
18 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
19 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
20 |
**
|
|
21 |
** If you have questions regarding the use of this file, please contact
|
|
22 |
** Nokia at developer.feedback@nokia.com.
|
|
23 |
**
|
|
24 |
****************************************************************************/
|
|
25 |
|
|
26 |
#include "hbinputservermethods_p.h"
|
|
27 |
|
|
28 |
#include <QDateTime>
|
|
29 |
#include <QBuffer>
|
|
30 |
#include <QFileSystemWatcher>
|
|
31 |
#include <QSharedMemory>
|
|
32 |
#include <hbinputsettingproxy.h>
|
|
33 |
#include <hbinputmodecache_p_p.h>
|
|
34 |
|
|
35 |
HbInputServerMethods::HbInputServerMethods(HbInputServer *server)
|
|
36 |
: mServer(server),
|
|
37 |
mSharedMethods(0),
|
|
38 |
mSharedMethodsModTime(0),
|
|
39 |
mMethods(0),
|
|
40 |
mMethodsSerialized(new QByteArray()),
|
|
41 |
mMethodPathWatcher(0)
|
|
42 |
{
|
|
43 |
}
|
|
44 |
|
|
45 |
HbInputServerMethods::~HbInputServerMethods()
|
|
46 |
{
|
|
47 |
delete mMethodsSerialized;
|
|
48 |
delete mSharedMethods;
|
|
49 |
delete mSharedMethodsModTime;
|
|
50 |
delete mMethods;
|
|
51 |
delete mMethodPathWatcher;
|
|
52 |
}
|
|
53 |
|
|
54 |
/*
|
|
55 |
Initializes input method sharing.
|
|
56 |
|
|
57 |
Creates shared memories for input method list and its latest modification time.
|
|
58 |
Initializes the list based on available input method plugins and updates the modification time.
|
|
59 |
|
|
60 |
Returns true on success, false if not able to create shared memories.
|
|
61 |
*/
|
|
62 |
bool HbInputServerMethods::initialize()
|
|
63 |
{
|
|
64 |
mServer->debugPrint("Initializing input mode cache");
|
|
65 |
// Create shared memory block with last modification time
|
|
66 |
mSharedMethodsModTime = new QSharedMemory(HbInputMethodListModTimeKey);
|
|
67 |
if (!mSharedMethodsModTime->create(sizeof(uint))) {
|
|
68 |
mServer->debugPrint("Unable to create method list mod time shared memory block!");
|
|
69 |
return false;
|
|
70 |
}
|
|
71 |
// Read mode data from disk to local structure
|
|
72 |
updateLocalInputMethodList();
|
|
73 |
// Create shared memory object for mode cache
|
|
74 |
mSharedMethods = new QSharedMemory(HbInputMethodListKey);
|
|
75 |
|
|
76 |
updateSharedMethodList();
|
|
77 |
|
|
78 |
startMonitoringMethodListChanges();
|
|
79 |
|
|
80 |
mServer->debugPrint("Input mode cache initialized");
|
|
81 |
|
|
82 |
return true;
|
|
83 |
}
|
|
84 |
|
|
85 |
/*
|
|
86 |
Updates shared input methods list based on local list, updates modification time.
|
|
87 |
*/
|
|
88 |
void HbInputServerMethods::updateSharedMethodList()
|
|
89 |
{
|
|
90 |
mServer->debugPrint("Updating shared memory for input methods");
|
|
91 |
if (!mSharedMethods->isAttached()) {
|
|
92 |
// If shared memory block is not attached, it should be created first
|
|
93 |
mServer->debugPrint("Creating shared memory for input methods");
|
|
94 |
mSharedMethods->create(mMethodsSerialized->size()+sizeof(int));
|
|
95 |
} else if (mSharedMethods->size() < static_cast<int>(mMethodsSerialized->size()+sizeof(int))) {
|
|
96 |
// If the shared memory block is too small, detach from it and recreate it
|
|
97 |
mServer->debugPrint("Re-creating shared memory for input methods");
|
|
98 |
mSharedMethods->detach();
|
|
99 |
bool success = mSharedMethods->create(mMethodsSerialized->size()+sizeof(int));
|
|
100 |
if (!success) {
|
|
101 |
mServer->debugPrint("Re-creating shared memory for input methods failed, error: "
|
|
102 |
+ mSharedMethods->errorString());
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
// Externalize data from local structure to shared memory
|
|
107 |
// Data size needs to be saved, since QSharedMemory is reserved in bigger blocks
|
|
108 |
// and we can't make a block of exactly the required size
|
|
109 |
mSharedMethods->lock();
|
|
110 |
*static_cast<int*>(mSharedMethods->data()) = mMethodsSerialized->size();
|
|
111 |
memcpy(static_cast<char*>(mSharedMethods->data())+sizeof(int), mMethodsSerialized->constData(), mMethodsSerialized->size());
|
|
112 |
mSharedMethods->unlock();
|
|
113 |
|
|
114 |
// Update modification time
|
|
115 |
mSharedMethodsModTime->lock();
|
|
116 |
*static_cast<uint*>(mSharedMethodsModTime->data()) = QDateTime::currentDateTime().toTime_t();
|
|
117 |
mSharedMethodsModTime->unlock();
|
|
118 |
mServer->debugPrint("Shared memory for input methods updated");
|
|
119 |
}
|
|
120 |
|
|
121 |
/*
|
|
122 |
Updates local copy of input methods list based on available input method plugins.
|
|
123 |
If path is given, updates only the methods in that directory.
|
|
124 |
*/
|
|
125 |
void HbInputServerMethods::updateLocalInputMethodList(const QString &path)
|
|
126 |
{
|
|
127 |
mServer->debugPrint("Updating local copy of methods list");
|
|
128 |
if (!mMethods) {
|
|
129 |
mMethods = new QList<HbInputMethodListItem>();
|
|
130 |
}
|
|
131 |
if (!path.isEmpty()) {
|
|
132 |
HbInputModeCachePrivate::readInputMethodDataFromDisk(mMethods, QDir(path));
|
|
133 |
} else {
|
|
134 |
HbInputModeCachePrivate::readInputMethodDataFromDisk(mMethods);
|
|
135 |
}
|
|
136 |
|
|
137 |
// Clear deleted items from the list
|
|
138 |
for (int i = mMethods->size()-1; i >= 0 ; --i) {
|
|
139 |
if (mMethods->at(i).toBeRemoved) {
|
|
140 |
mMethods->removeAt(i);
|
|
141 |
}
|
|
142 |
}
|
|
143 |
|
|
144 |
mServer->debugPrint("Number of methods: " + QString::number(mMethods->count()));
|
|
145 |
|
|
146 |
QBuffer buffer(mMethodsSerialized);
|
|
147 |
buffer.open(QIODevice::WriteOnly);
|
|
148 |
|
|
149 |
QDataStream out(&buffer);
|
|
150 |
out << *mMethods;
|
|
151 |
mServer->debugPrint("Method list updated, new size: " + QString::number(mMethodsSerialized->size()));
|
|
152 |
}
|
|
153 |
|
|
154 |
/*
|
|
155 |
Sets up directory watchers etc. to monitor changes in input method plugins.
|
|
156 |
*/
|
|
157 |
void HbInputServerMethods::startMonitoringMethodListChanges()
|
|
158 |
{
|
|
159 |
mServer->debugPrint("Setting up change monitoring for input methods");
|
|
160 |
if (mMethodPathWatcher) {
|
|
161 |
mServer->debugPrint("Directory watcher already exists, deleting");
|
|
162 |
delete mMethodPathWatcher;
|
|
163 |
}
|
|
164 |
|
|
165 |
mMethodPathWatcher = new QFileSystemWatcher();
|
|
166 |
|
|
167 |
// Get the list of plugin paths, remove ROM drive from the paths if it's there
|
|
168 |
QStringList directoriesToWatch = HbInputSettingProxy::inputMethodPluginPaths();
|
|
169 |
foreach(const QString& path, directoriesToWatch) {
|
|
170 |
if (path.length() > 0 && path.at(0) == 'z') {
|
|
171 |
directoriesToWatch.removeOne(path);
|
|
172 |
}
|
|
173 |
}
|
|
174 |
|
|
175 |
// TODO: Create directories to watch on non-removable drives (c, e on symbian) if they don't exist
|
|
176 |
mMethodPathWatcher->addPaths(directoriesToWatch);
|
|
177 |
connect(mMethodPathWatcher, SIGNAL(directoryChanged(QString)), this, SLOT(changeInMethodDirectory(QString)));
|
|
178 |
mServer->debugPrint("Input method path watcher setup done");
|
|
179 |
|
|
180 |
// TODO: Set up MMC handling
|
|
181 |
}
|
|
182 |
|
|
183 |
/*
|
|
184 |
Slot directory watchers call when something changes.
|
|
185 |
*/
|
|
186 |
void HbInputServerMethods::changeInMethodDirectory(const QString &path)
|
|
187 |
{
|
|
188 |
mServer->debugPrint("Change in method path detected, path: " + path);
|
|
189 |
updateLocalInputMethodList(path);
|
|
190 |
updateSharedMethodList();
|
|
191 |
}
|