1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0"" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "cpthemeclient_p_p.h" |
|
19 #include <QLocalSocket> |
|
20 #include <QProcess> |
|
21 #include <QFile> |
|
22 #include <QCoreApplication> |
|
23 #include <QDebug> |
|
24 #include <QDir> |
|
25 |
|
26 #include <hbinstance.h> |
|
27 |
|
28 |
|
29 #define WAIT_TIME_TO_CONNECT_TO_SERVER 500 |
|
30 #define WAIT_TIME_TO_START_SERVER 5000 |
|
31 /** |
|
32 * Constructor |
|
33 */ |
|
34 CpThemeClientPrivate::CpThemeClientPrivate():clientConnected( false ),localSocket( new QLocalSocket() ) |
|
35 { |
|
36 #ifdef THEME_SERVER_TRACES |
|
37 qDebug() << Q_FUNC_INFO ; |
|
38 #endif |
|
39 } |
|
40 |
|
41 /** |
|
42 * connectToServer |
|
43 */ |
|
44 bool CpThemeClientPrivate::connectToServer() |
|
45 { |
|
46 localSocket->connectToServer(THEME_SERVER_NAME); |
|
47 // This logic needs to be improved |
|
48 bool success = localSocket->waitForConnected( WAIT_TIME_TO_CONNECT_TO_SERVER ); |
|
49 |
|
50 #ifdef THEME_SERVER_TRACES |
|
51 qDebug() << Q_FUNC_INFO << "Socket Connect status: " << success; |
|
52 #endif |
|
53 |
|
54 // Stub shouldn't try starting the theme server yet again. |
|
55 #if 0 |
|
56 if(!success) { |
|
57 QProcess *newProcess = new QProcess(); |
|
58 newProcess->start(SERVERFILEPATH); |
|
59 success = newProcess->waitForStarted( WAIT_TIME_TO_START_SERVER ); |
|
60 #ifdef THEME_SERVER_TRACES |
|
61 qDebug() << Q_FUNC_INFO << "Server Start Status: " << success << "Error = " << newProcess->error (); |
|
62 #endif |
|
63 |
|
64 // If server started |
|
65 if (success) { |
|
66 // ToDo: This is to wait for server to start running. Logic needs to be improved. |
|
67 newProcess->waitForFinished(3000); |
|
68 #ifdef THEME_SERVER_TRACES |
|
69 qDebug() <<Q_FUNC_INFO<< " Server Start Wait is over" ; |
|
70 #endif |
|
71 localSocket->connectToServer(THEME_SERVER_NAME); |
|
72 success = localSocket->waitForConnected(); |
|
73 #ifdef THEME_SERVER_TRACES |
|
74 qDebug() <<Q_FUNC_INFO<< "socketconnected : "<<success; |
|
75 #endif |
|
76 } |
|
77 } |
|
78 #endif |
|
79 if (success) { |
|
80 connect(localSocket, SIGNAL(readyRead()), this, SLOT(themeChanged())); |
|
81 } |
|
82 return clientConnected = success; |
|
83 } |
|
84 |
|
85 /** |
|
86 * isConnected |
|
87 */ |
|
88 bool CpThemeClientPrivate::isConnected() const |
|
89 { |
|
90 return clientConnected; |
|
91 } |
|
92 |
|
93 /** |
|
94 * Destructor |
|
95 */ |
|
96 CpThemeClientPrivate::~CpThemeClientPrivate() |
|
97 { |
|
98 localSocket->disconnectFromServer(); |
|
99 delete localSocket; |
|
100 } |
|
101 |
|
102 /** |
|
103 * CpThemeClientPrivate::themeChanged() |
|
104 */ |
|
105 void CpThemeClientPrivate::themeChanged() |
|
106 { |
|
107 #ifdef THEME_SERVER_TRACES |
|
108 qDebug() << Q_FUNC_INFO; |
|
109 #endif |
|
110 |
|
111 QByteArray inputByteArray = localSocket->readAll(); |
|
112 QDataStream inputDataStream(inputByteArray); |
|
113 int request; |
|
114 inputDataStream >> request; |
|
115 |
|
116 #ifdef THEME_SERVER_TRACES |
|
117 qDebug() << Q_FUNC_INFO << "recognizer: "<<request; |
|
118 #endif |
|
119 |
|
120 if(EThemeSelection==request) { |
|
121 QString themeName; |
|
122 handleThemeChangeRequest(inputDataStream); |
|
123 } |
|
124 #if 0 |
|
125 // TODO determine if we need this for the control panel app |
|
126 if(EThemeContentUpdate == request) { |
|
127 hbInstance->theme()->d_ptr->clearCache(); |
|
128 } |
|
129 #endif |
|
130 } |
|
131 |
|
132 /** |
|
133 * CpThemeClientPrivate::handleThemeChangeRequest() |
|
134 */ |
|
135 void CpThemeClientPrivate::handleThemeChangeRequest(QDataStream &dataStream) |
|
136 { |
|
137 QString themeName; |
|
138 dataStream >> themeName; |
|
139 #ifdef THEME_SERVER_TRACES |
|
140 qDebug() << Q_FUNC_INFO <<"themeName is : " <<themeName; |
|
141 #endif |
|
142 |
|
143 if(!(hbInstance->theme()->name() == themeName)) { |
|
144 #ifdef THEME_SERVER_TRACES |
|
145 qDebug() << Q_FUNC_INFO <<"themeChanged(): called"; |
|
146 #endif |
|
147 #if 0 |
|
148 // TODO determine if we need this for the control panel app |
|
149 hbInstance->theme()->d_ptr->handleThemeChange(); |
|
150 #endif |
|
151 } |
|
152 } |
|
153 |
|
154 /** |
|
155 * changeTheme |
|
156 */ |
|
157 bool CpThemeClientPrivate::changeTheme(const QString &newTheme) |
|
158 { |
|
159 #ifdef THEME_CHANGER_TRACES |
|
160 qDebug() <<"ThemeClientQt::changeTheme("<<newTheme<<") called"; |
|
161 #endif |
|
162 if( (themeName==newTheme) || (newTheme.isEmpty()) ) { |
|
163 #ifdef THEME_CHANGER_TRACES |
|
164 qDebug() <<"ThemeClientQt:: return Sametheme applied"; |
|
165 #endif |
|
166 return false; |
|
167 } |
|
168 QByteArray outputByteArray; |
|
169 QDataStream outputDataStream(&outputByteArray, QIODevice::WriteOnly); |
|
170 ThemeServerRequest requestType = EThemeSelection; |
|
171 outputDataStream << (int)requestType; |
|
172 outputDataStream << newTheme; |
|
173 themeName = newTheme; |
|
174 int expected = outputByteArray.size(); |
|
175 int count = localSocket->write(outputByteArray); |
|
176 #ifdef THEME_CHANGER_TRACES |
|
177 qDebug()<<"ThemeClientQt::ThemeName written to server"; |
|
178 #endif |
|
179 localSocket->flush(); |
|
180 return count == expected; |
|
181 } |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|