qthighway/examples/serviceapp/src/serviceapp.cpp
changeset 1 2b40d63a9c3d
child 4 90517678cc4f
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 * 
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not, 
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
       
    19 *
       
    20 */
       
    21 
       
    22 #include "xqservicelog.h"
       
    23 
       
    24 #include <QApplication>
       
    25 #include <QKeyEvent>
       
    26 #include <QLabel>
       
    27 #include <QVBoxLayout>
       
    28 #include <QStackedWidget>
       
    29 #include <QImageReader>
       
    30 #include <QDebug>
       
    31 #include <QTimer>
       
    32 #include <QPushButton>
       
    33 #include <QLineEdit>
       
    34 #include <QListView>
       
    35 #include <QMessageBox>
       
    36 #include <xqsharablefile.h>
       
    37 
       
    38 #include "serviceapp.h"
       
    39 #include <xqserviceutil.h>
       
    40 
       
    41 ServiceApp::ServiceApp(QWidget *parent, Qt::WFlags f)
       
    42     : QWidget(parent, f),
       
    43       mService(NULL),
       
    44       mUriService(NULL),
       
    45       mFileService(NULL)
       
    46 {
       
    47     XQSERVICE_DEBUG_PRINT("ServiceApp::ServiceApp");
       
    48     if (XQServiceUtil::isService())
       
    49     {
       
    50         mService = new DialerService(this);
       
    51         mUriService = new UriService(this);
       
    52         mFileService = new FileService(this);
       
    53     }
       
    54     /* Adjust the palette */
       
    55 #if defined(Q_WS_S60)
       
    56     QPalette p = qApp->palette();
       
    57     QColor color(192,192,192);
       
    58     QColor bg(201,250,250);
       
    59     p.setColor(QPalette::Highlight, color.lighter(200));
       
    60     p.setColor(QPalette::Text, Qt::black);
       
    61     p.setColor(QPalette::Base, bg);
       
    62     p.setColor(QPalette::WindowText, Qt::black);
       
    63     p.setColor(QPalette::Window, bg);
       
    64     p.setColor(QPalette::ButtonText, Qt::black);
       
    65     p.setColor(QPalette::Button, color.lighter(150));
       
    66     p.setColor(QPalette::Link, QColor(240,40,40));
       
    67 
       
    68     qApp->setPalette(p);
       
    69 #endif
       
    70 
       
    71     QPushButton *quitButton = new QPushButton(tr("Quit"));
       
    72     QPushButton *answerButton = new QPushButton(tr("Async Answer"));
       
    73     connect(quitButton, SIGNAL(clicked()), this, SLOT(quit()));
       
    74     connect(answerButton, SIGNAL(clicked()), this, SLOT(answer()));
       
    75 
       
    76     /*
       
    77     mEndCallButton = new QPushButton(tr("End Call"));
       
    78     mEndCallButton->setEnabled(false);
       
    79     connect(mEndCallButton, SIGNAL(clicked()), this, SLOT(endCall()));
       
    80     */
       
    81     bool isService = XQServiceUtil::isService();
       
    82     
       
    83     QString t = "SERVICEAPP:\n";
       
    84     t = t + (isService ?  "    LAUNCHED AS SERVICE\n" : "    LAUNCHED NORMALLY\n");
       
    85     t = t + (XQServiceUtil::isEmbedded() ? "    EMBEDDED\n" : "    NOT EMBEDDED\n");
       
    86     
       
    87     QStringList args = QApplication::arguments();
       
    88     foreach (QString arg, args)
       
    89     {
       
    90         t += "cmdline arg=" + arg + "\n";
       
    91     }
       
    92     
       
    93     QLabel *title = new QLabel(t);
       
    94 
       
    95     mLabel = new QLabel("");
       
    96     mNumber = new QLineEdit("");
       
    97     
       
    98     QVBoxLayout *vl = new QVBoxLayout;
       
    99     vl->setMargin(0);
       
   100     vl->setSpacing(0);
       
   101 
       
   102     vl->addWidget(answerButton);
       
   103     vl->addWidget(quitButton);
       
   104     vl->addWidget(title);
       
   105     vl->addWidget(mLabel);
       
   106     vl->addWidget(mNumber);
       
   107 
       
   108     setLayout(vl);
       
   109    
       
   110 #if defined(Q_WS_X11) || defined(Q_WS_WIN)
       
   111     setFixedSize(QSize(360,640)); // nHD
       
   112 #elif defined(Q_WS_S60)
       
   113     // showMaximized();
       
   114     showFullScreen();
       
   115 #endif
       
   116 //    new DialerService(this);
       
   117 }
       
   118 
       
   119 
       
   120 ServiceApp::~ServiceApp()
       
   121 {
       
   122     XQSERVICE_DEBUG_PRINT("ServiceApp::~ServiceApp");
       
   123     delete mService;
       
   124     delete mUriService;
       
   125     delete mFileService;
       
   126 }
       
   127 
       
   128 void ServiceApp::quit()
       
   129 {
       
   130     XQSERVICE_DEBUG_PRINT("ServiceApp::quit");
       
   131     qApp->quit();
       
   132 }
       
   133 
       
   134 void ServiceApp::answer()
       
   135 {
       
   136     XQSERVICE_DEBUG_PRINT("ServiceApp::answer");
       
   137     if (mService && mService->asyncAnswer())
       
   138     {
       
   139         // connect(mService, SIGNAL(returnValueDelivered()), this, SLOT(handleAnswerDelivered()));
       
   140         mService->complete(mNumber->text());
       
   141     }
       
   142     if (mUriService && mUriService->asyncAnswer())
       
   143     {
       
   144         connect(mUriService, SIGNAL(returnValueDelivered()), this, SLOT(handleAnswerDelivered()));
       
   145         mUriService->complete(true);
       
   146     }
       
   147     if (mFileService && mFileService->asyncAnswer())
       
   148     {
       
   149         connect(mUriService, SIGNAL(returnValueDelivered()), this, SLOT(handleAnswerDelivered()));
       
   150         mUriService->complete(true);
       
   151     }
       
   152 }
       
   153 
       
   154 void ServiceApp::handleAnswerDelivered()
       
   155 {
       
   156     XQSERVICE_DEBUG_PRINT("ServiceApp::handleAnswerDelivered");
       
   157     quit();
       
   158     
       
   159 }
       
   160 
       
   161 
       
   162 void ServiceApp::endCall()
       
   163 {
       
   164     XQSERVICE_DEBUG_PRINT("ServiceApp::endCall");
       
   165     //QVBoxLayout *vl = qobject_cast<QVBoxLayout *>(layout()) ;
       
   166     //vl->removeWidget(mEndCallButton);
       
   167 
       
   168     //XQServiceUtil::toBackground(true);
       
   169 }
       
   170 
       
   171 void ServiceApp::setLabelNumber(QString label,QString number)
       
   172 {
       
   173     XQSERVICE_DEBUG_PRINT("ServiceApp::setLabelNumber");
       
   174     mLabel->setText(label);
       
   175     mNumber->setText(number);
       
   176 }
       
   177 
       
   178 // ----------DialerService---------------
       
   179 
       
   180 DialerService::DialerService(ServiceApp* parent)
       
   181 : XQServiceProvider(QLatin1String("com.nokia.services.serviceapp.Dialer"),parent),
       
   182     mServiceApp(parent),
       
   183     mAsyncReqId(-1),
       
   184     mAsyncAnswer(false)
       
   185 {
       
   186     XQSERVICE_DEBUG_PRINT("DialerService::DialerService");
       
   187     publishAll();
       
   188     connect(this, SIGNAL(returnValueDelivered()), parent, SLOT(handleAnswerDelivered()));
       
   189 }
       
   190 
       
   191 DialerService::~DialerService()
       
   192 {
       
   193     XQSERVICE_DEBUG_PRINT("DialerService::~DialerService");
       
   194 }
       
   195 
       
   196 void DialerService::complete(QString number)
       
   197 {
       
   198     if (mAsyncReqId == -1)
       
   199         return;
       
   200     XQSERVICE_DEBUG_PRINT("DialerService::complete");
       
   201     completeRequest(mAsyncReqId,number.toInt());
       
   202 }
       
   203 
       
   204 int DialerService::dial(const QString& number, bool asyncAnswer)
       
   205 {
       
   206     XQSERVICE_DEBUG_PRINT("DialerService::dial: %s,%d", qPrintable(number), asyncAnswer);
       
   207     XQRequestInfo info = requestInfo();
       
   208     
       
   209     XQSERVICE_DEBUG_PRINT("\tRequest info: sid=%X,vid=%X", info.clientSecureId(), info.clientVendorId());
       
   210     QSet<int> caps = info.clientCapabilities();
       
   211     QSetIterator<int> i(caps);
       
   212     while (i.hasNext())
       
   213         qDebug() << "Has capability " << i.next();    
       
   214     XQSERVICE_DEBUG_PRINT("\tRequest info: embed=%d,sync=%d", info.isEmbedded(), info.isSynchronous());
       
   215     
       
   216     QString label = "Dialer::dial:\n";
       
   217     label += QString("number=%1\n").arg(number);
       
   218     label += QString("asyncAnswer=%1\n").arg(asyncAnswer);
       
   219     
       
   220     mAsyncAnswer = asyncAnswer; 
       
   221     mNumber = number ;
       
   222     mServiceApp->setLabelNumber(label, number);
       
   223     int ret = 0;
       
   224     if (mAsyncAnswer)
       
   225     {
       
   226         mAsyncReqId = setCurrentRequestAsync();
       
   227         connect(this, SIGNAL(clientDisconnected()), this, SLOT(handleClientDisconnect()));
       
   228     }
       
   229     else
       
   230     {
       
   231         ret = number.toInt();
       
   232     }
       
   233     return ret;
       
   234 }
       
   235 
       
   236 void DialerService::handleClientDisconnect()
       
   237 {
       
   238     XQSERVICE_DEBUG_PRINT("DialerService::handleClientDisconnect");
       
   239     // Just quit service application if client ends
       
   240     mAsyncAnswer = false;
       
   241     mServiceApp->quit();
       
   242 }
       
   243 
       
   244 
       
   245 // ----------UriService---------------
       
   246 
       
   247 UriService::UriService(ServiceApp* parent)
       
   248 : XQServiceProvider(QLatin1String("com.nokia.services.serviceapp.com.nokia.symbian.IUriView"),parent),
       
   249     mServiceApp(parent),
       
   250     mAsyncReqId(-1),
       
   251     mAsyncAnswer(false)
       
   252     
       
   253 {
       
   254     XQSERVICE_DEBUG_PRINT("UriService::UriService");
       
   255     publishAll();
       
   256 }
       
   257 
       
   258 UriService::~UriService()
       
   259 {
       
   260     XQSERVICE_DEBUG_PRINT("UriService::~UriService");
       
   261 }
       
   262 
       
   263 void UriService::complete(bool ok)
       
   264 {
       
   265     if (mAsyncReqId == -1)
       
   266         return;
       
   267     XQSERVICE_DEBUG_PRINT("UriService::complete");
       
   268     completeRequest(mAsyncReqId, QVariant(mRetValue));
       
   269 }
       
   270 
       
   271 bool UriService::view(const QString& uri)
       
   272 {
       
   273     XQSERVICE_DEBUG_PRINT("UriService::view(1)");
       
   274     return view(uri, true);
       
   275 }
       
   276 
       
   277 bool UriService::view(const QString& uri, bool retValue)
       
   278 {
       
   279     XQSERVICE_DEBUG_PRINT("UriService::view(2)");
       
   280     QString label = "IUri::view\n:";
       
   281     label += QString ("Uri=%1\n").arg(uri);
       
   282     label += QString ("retValue=%1\n").arg(retValue);
       
   283     QString param = QString ("retValue=%1\n").arg(retValue);
       
   284 
       
   285     mAsyncAnswer = !XQServiceUtil::isEmbedded();;
       
   286     mRetValue = retValue;
       
   287     mServiceApp->setLabelNumber(label,param);
       
   288     if (mAsyncAnswer)
       
   289     {
       
   290         mAsyncReqId = setCurrentRequestAsync();
       
   291         connect(this, SIGNAL(clientDisconnected()), this, SLOT(handleClientDisconnect()));
       
   292     }
       
   293     
       
   294     return retValue;
       
   295 }
       
   296 
       
   297 void UriService::handleClientDisconnect()
       
   298 {
       
   299     XQSERVICE_DEBUG_PRINT("UriService::handleClientDisconnect");
       
   300 
       
   301     // Just quit application
       
   302     mAsyncAnswer = false;
       
   303     mServiceApp->quit();
       
   304 }
       
   305 
       
   306 
       
   307 // ----------FileService---------------
       
   308 
       
   309 FileService::FileService(ServiceApp* parent)
       
   310 : XQServiceProvider(QLatin1String("com.nokia.services.serviceapp.com.nokia.symbian.IFileView"),parent),
       
   311     mServiceApp(parent),
       
   312     mAsyncReqId(-1),
       
   313     mAsyncAnswer(false)
       
   314 
       
   315 {
       
   316     XQSERVICE_DEBUG_PRINT("FileService::FileService");
       
   317     publishAll();
       
   318 }
       
   319 
       
   320 FileService::~FileService()
       
   321 {
       
   322     XQSERVICE_DEBUG_PRINT("FileService::~FileService");
       
   323 }
       
   324 
       
   325 void FileService::complete(bool ok)
       
   326 {
       
   327     if (mAsyncReqId == -1)
       
   328         return;
       
   329     XQSERVICE_DEBUG_PRINT("FileService::complete");
       
   330     completeRequest(mAsyncReqId, QVariant(ok));
       
   331 }
       
   332 
       
   333 bool FileService::view(QString file)
       
   334 {
       
   335     XQSERVICE_DEBUG_PRINT("FileService::view(QString)");
       
   336     QString label = "IFile::view\n:";
       
   337     QString param = QString ("File=%1\n").arg(file);
       
   338 
       
   339     mAsyncAnswer = !XQServiceUtil::isEmbedded();;
       
   340     mServiceApp->setLabelNumber(label,param);
       
   341     if (mAsyncAnswer)
       
   342     {
       
   343         mAsyncReqId = setCurrentRequestAsync();
       
   344         connect(this, SIGNAL(clientDisconnected()), this, SLOT(handleClientDisconnect()));
       
   345     }
       
   346     
       
   347     return true;
       
   348 }
       
   349 
       
   350 
       
   351 bool FileService::view(XQSharableFile sf)
       
   352 {
       
   353     XQSERVICE_DEBUG_PRINT("FileService::view(XQSharebleFile)");
       
   354     QString label = "IFile::view\n:";
       
   355     QString param = QString ("File=%1\n").arg(sf.fileName());
       
   356 
       
   357     RFile file;
       
   358     bool ok = sf.getHandle( file );
       
   359     if (ok)
       
   360     {
       
   361         HBufC8* data = HBufC8::NewL(100);
       
   362         TPtr8 ptr = data->Des();
       
   363         TInt err  = file.Read( ptr );
       
   364         QString text = QString::fromUtf8((const char *)(data->Ptr()), data->Length());
       
   365         XQSERVICE_DEBUG_PRINT("FileService::file read,%d,%s", err, qPrintable(text));
       
   366         sf.close();
       
   367         delete data;
       
   368     }
       
   369 
       
   370     mAsyncAnswer = !XQServiceUtil::isEmbedded();;
       
   371     mServiceApp->setLabelNumber(label,param);
       
   372     if (mAsyncAnswer)
       
   373     {
       
   374         mAsyncReqId = setCurrentRequestAsync();
       
   375         connect(this, SIGNAL(clientDisconnected()), this, SLOT(handleClientDisconnect()));
       
   376     }
       
   377     return true;
       
   378 }
       
   379 
       
   380 
       
   381 void FileService::handleClientDisconnect()
       
   382 {
       
   383     XQSERVICE_DEBUG_PRINT("FileService::handleClientDisconnect");
       
   384 
       
   385     // Just quit application
       
   386     mAsyncAnswer = false;
       
   387     mServiceApp->quit();
       
   388 }
       
   389