tools/runonphone/symbianutils/communicationstarter.h
branchRCL_3
changeset 8 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 8:3f74d0d4af4c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the tools applications of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef COMMUNICATIONSTARTER_H
       
    43 #define COMMUNICATIONSTARTER_H
       
    44 
       
    45 #include "symbianutils_global.h"
       
    46 
       
    47 #include <QtCore/QSharedPointer>
       
    48 #include <QtCore/QObject>
       
    49 
       
    50 namespace trk {
       
    51 class TrkDevice;
       
    52 class BluetoothListener;
       
    53 struct BaseCommunicationStarterPrivate;
       
    54 
       
    55 /* BaseCommunicationStarter: A QObject that repeatedly tries to open a
       
    56  * trk device until a connection succeeds or a timeout occurs (emitting
       
    57  * signals), allowing to do something else in the foreground (local event loop
       
    58  * [say QMessageBox] or some asynchronous operation). If the initial
       
    59  * connection attempt in start() fails, the
       
    60  * virtual initializeStartupResources() is called to initialize resources
       
    61  * required to pull up the communication (namely Bluetooth listeners).
       
    62  * The base class can be used as is to prompt the user to launch App TRK for a
       
    63  * serial communication as this requires no further resource setup. */
       
    64 
       
    65 class SYMBIANUTILS_EXPORT BaseCommunicationStarter : public QObject {
       
    66     Q_OBJECT
       
    67     Q_DISABLE_COPY(BaseCommunicationStarter)
       
    68 public:
       
    69     typedef QSharedPointer<TrkDevice> TrkDevicePtr;
       
    70 
       
    71     enum State { Running, Connected, TimedOut };
       
    72 
       
    73     explicit BaseCommunicationStarter(const TrkDevicePtr& trkDevice, QObject *parent = 0);
       
    74     virtual ~BaseCommunicationStarter();
       
    75 
       
    76     int intervalMS() const;
       
    77     void setIntervalMS(int i);
       
    78 
       
    79     int attempts() const;
       
    80     void setAttempts(int a);
       
    81 
       
    82     QString device() const;
       
    83 
       
    84     State state() const;
       
    85     QString errorString() const;
       
    86 
       
    87     enum StartResult {
       
    88         Started,               // Starter is now running.
       
    89         ConnectionSucceeded,   /* Initial connection attempt succeeded,
       
    90                                 * no need to keep running. */
       
    91         StartError             // Error occurred during start.
       
    92     };
       
    93 
       
    94     StartResult start();
       
    95 
       
    96 signals:
       
    97     void connected();
       
    98     void timeout();
       
    99     void message(const QString &);
       
   100 
       
   101 private slots:
       
   102     void slotTimer();
       
   103 
       
   104 protected:
       
   105     virtual bool initializeStartupResources(QString *errorMessage);
       
   106 
       
   107 private:
       
   108     inline void stopTimer();
       
   109 
       
   110     BaseCommunicationStarterPrivate *d;
       
   111 };
       
   112 
       
   113 /* AbstractBluetoothStarter: Repeatedly tries to open a trk Bluetooth
       
   114  * device. Note that in case a Listener is already running mode, the
       
   115  * connection will succeed immediately.
       
   116  * initializeStartupResources() is implemented to fire up the listener.
       
   117  * Introduces a new virtual createListener() that derived classes must
       
   118  * implement as a factory function that creates and sets up the
       
   119  * listener (mode, message connection, etc). */
       
   120 
       
   121 class SYMBIANUTILS_EXPORT AbstractBluetoothStarter : public BaseCommunicationStarter {
       
   122     Q_OBJECT
       
   123     Q_DISABLE_COPY(AbstractBluetoothStarter)
       
   124 public:
       
   125 
       
   126 protected:
       
   127     explicit AbstractBluetoothStarter(const TrkDevicePtr& trkDevice, QObject *parent = 0);
       
   128 
       
   129     // Implemented to fire up the listener.
       
   130     virtual bool initializeStartupResources(QString *errorMessage);
       
   131     // New virtual: Overwrite to create and parametrize the listener.
       
   132     virtual BluetoothListener *createListener() = 0;
       
   133 };
       
   134 
       
   135 /* ConsoleBluetoothStarter: Convenience class for console processes. Creates a
       
   136  * listener in "Listen" mode with the messages redirected to standard output. */
       
   137 
       
   138 class SYMBIANUTILS_EXPORT ConsoleBluetoothStarter : public AbstractBluetoothStarter {
       
   139     Q_OBJECT
       
   140     Q_DISABLE_COPY(ConsoleBluetoothStarter)
       
   141 public:
       
   142     static bool startBluetooth(const TrkDevicePtr& trkDevice,
       
   143                                QObject *listenerParent,
       
   144                                int attempts,
       
   145                                QString *errorMessage);
       
   146 
       
   147 protected:
       
   148     virtual BluetoothListener *createListener();
       
   149 
       
   150 private:
       
   151     explicit ConsoleBluetoothStarter(const TrkDevicePtr& trkDevice,
       
   152                                      QObject *listenerParent,
       
   153                                      QObject *parent = 0);
       
   154 
       
   155     QObject *m_listenerParent;
       
   156 };
       
   157 
       
   158 } // namespace trk
       
   159 
       
   160 #endif // COMMUNICATIONSTARTER_H