~renatofilho/buteo-syncfw/fix-1509053

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
/*
 * This file is part of buteo-syncfw package
 *
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 *
 * Contact: Sateesh Kavuri <sateesh.kavuri@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.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */
#ifndef NETWORKMANAGER_H_
#define NETWORKMANAGER_H_

#include <QNetworkSession>
#include <QTimer>
#include "SyncCommonDefs.h"

class QNetworkConfigurationManager;

namespace Buteo
{

    /*! \brief Class for managing network sessions
    *
    * This class provides APIs to open and close network sessions. It internally
    * uses QNetworkSession set of classes to manage network sessions. The user
    * while creating a new session can choose whether to pop-up the internet
    * connectivity dialog or not. The class also signals about online status of
    * the device.
    */
    class NetworkManager : public QObject
    {
        Q_OBJECT
        public:
            /*! \brief Constructor
             *
             * @param parent Parent object
             */
            NetworkManager(QObject *parent = 0);

            /*! \brief Destructor
             *
             */
            ~NetworkManager();

            /*! \brief Returns if the device is currently online, i.e, a data
             * sessions is possible.
             *
             * @return True if device is online, false if not
             */
            bool isOnline();

            /*! \brief Returns the type of connection used by the device.
             *

             * @return Sync::InternetConnectionType the type of connection.
             */
            Sync::InternetConnectionType connectionType() const;

            /*! \brief Connects a new network session. If a session was already
             * open, the signal connectionSuccess will be emitted immediately,
             * else the function will return and the signal connectionSuccess or
             * connectionError will be emitted accordingly. The caller can
             * choose whether to show the network connectivity dialog, or just
             * open the default network configuration in the background using
             * the parameter connetInBackground.
             *
             * @param connectInBackground If true, the connection is opened
             * without popping up the connetion dialog
             */
            void connectSession(bool connectInBackground = false);

            /*! \brief Disconnects an open session
             *
             */
            void disconnectSession();
signals:
            /*! \brief This signal is emitted when the device's online status
             * changes
             *
             * @param aConnected If true, the device is online
             */
            void statusChanged(bool aConnected, Sync::InternetConnectionType aType);

            /*! \brief This signal is emitted when a network session gets
             * connected
             *
             */
            void connectionSuccess();

            /*! \brief This signal is emitted when opening a network session
             * fails
             *
             */
            void connectionError();
        private:
            QNetworkConfigurationManager    *m_networkConfigManager;    // QT network configuration manager
            QNetworkSession                 *m_networkSession;          // QT network session
            static bool                     m_isSessionActive;          // Flag to indicate if a network session is active
            bool                            m_isOnline;                 // Flag to indicate if the device is online
            static int                      m_refCount;                 // Reference counter for number of open connections
            bool                            m_errorEmitted;             // Network error emited flag
            QTimer                          *m_sessionTimer;
            Sync::InternetConnectionType    m_connectionType;
            QTimer                          m_idleRefreshTimer;

private slots:
            void slotSessionState(QNetworkSession::State status);
            void slotSessionError(QNetworkSession::SessionError error);
            void sessionConnectionTimeout();
            void slotConfigurationChanged();
            void idleRefresh();
    };
}

#endif//NETWORKMANAGER_H_