Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
139 andreas 1
/*
2
 * Copyright (C) 2022 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
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 General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include <QPushButton>
20
#include <QToolButton>
21
#include <QLabel>
22
 
23
#include "tqtphone.h"
140 andreas 24
#include "tpagemanager.h"
139 andreas 25
#include "terror.h"
26
#include "ui_tqtphone.h"
27
 
140 andreas 28
using std::vector;
29
using std::string;
30
using std::map;
31
using std::pair;
32
 
33
extern TPageManager *gPageManager;              //!< The pointer to the global defined main class.
34
 
139 andreas 35
TQtPhone::TQtPhone(QWidget* parent)
36
    : QDialog(parent),
37
      ui(new Ui::TQtPhone)
38
{
39
    DECL_TRACER("TQtPhone::TQtPhone(QWidget* parent)");
40
 
41
    ui->setupUi(this);
42
 
141 andreas 43
    ui->label_Number->setText(mNumber);
139 andreas 44
    ui->label_Status->setText("");
45
    ui->toolButton_Call->setText("");
46
    ui->toolButton_Call->setIcon(QIcon(":images/pickup.png"));
47
 
142 andreas 48
//    connect(ui->pushButton_Clear, &QAbstractButton::pressed, this, &TQtPhone::on_pushButton_Clear_clicked);
49
//    connect(ui->toolButton_Call, &QAbstractButton::pressed, this, &TQtPhone::on_toolButton_Call_clicked);
139 andreas 50
 
142 andreas 51
//    connect(ui->pushButton_Exit, &QAbstractButton::pressed, this, &TQtPhone::on_pushButton_Exit_clicked);
139 andreas 52
}
53
 
54
TQtPhone::~TQtPhone()
55
{
56
    DECL_TRACER("TQtPhone::~TQtPhone()");
57
 
58
    delete ui;
59
}
60
 
61
void TQtPhone::on_pushButton_0_clicked()
62
{
63
    DECL_TRACER("TQtPhone::on_pushButton_0_clicked()");
64
 
65
    mNumber += "0";
66
    ui->label_Number->setText(mNumber);
67
}
68
 
69
void TQtPhone::on_pushButton_1_clicked()
70
{
71
    DECL_TRACER("TQtPhone::on_pushButton_1_clicked()");
72
 
73
    mNumber += "1";
74
    ui->label_Number->setText(mNumber);
75
}
76
 
77
void TQtPhone::on_pushButton_2_clicked()
78
{
79
    DECL_TRACER("TQtPhone::on_pushButton_2_clicked()");
80
 
81
    mNumber += "2";
82
    ui->label_Number->setText(mNumber);
83
}
84
 
85
void TQtPhone::on_pushButton_3_clicked()
86
{
87
    DECL_TRACER("TQtPhone::on_pushButton_3_clicked()");
88
 
89
    mNumber += "3";
90
    ui->label_Number->setText(mNumber);
91
}
92
 
93
void TQtPhone::on_pushButton_4_clicked()
94
{
95
    DECL_TRACER("TQtPhone::on_pushButton_4_clicked()");
96
 
97
    mNumber += "4";
98
    ui->label_Number->setText(mNumber);
99
}
100
 
101
void TQtPhone::on_pushButton_5_clicked()
102
{
103
    DECL_TRACER("TQtPhone::on_pushButton_5_clicked()");
104
 
105
    mNumber += "5";
106
    ui->label_Number->setText(mNumber);
107
}
108
 
109
void TQtPhone::on_pushButton_6_clicked()
110
{
111
    DECL_TRACER("TQtPhone::on_pushButton_6_clicked()");
112
 
113
    mNumber += "6";
114
    ui->label_Number->setText(mNumber);
115
}
116
 
117
void TQtPhone::on_pushButton_7_clicked()
118
{
119
    DECL_TRACER("TQtPhone::on_pushButton_7_clicked()");
120
 
121
    mNumber += "7";
122
    ui->label_Number->setText(mNumber);
123
}
124
 
125
void TQtPhone::on_pushButton_8_clicked()
126
{
127
    DECL_TRACER("TQtPhone::on_pushButton_8_clicked()");
128
 
129
    mNumber += "8";
130
    ui->label_Number->setText(mNumber);
131
}
132
 
133
void TQtPhone::on_pushButton_9_clicked()
134
{
135
    DECL_TRACER("TQtPhone::on_pushButton_9_clicked()");
136
 
137
    mNumber += "9";
138
    ui->label_Number->setText(mNumber);
139
}
140
 
141
void TQtPhone::on_pushButton_Hash_clicked()
142
{
143
    DECL_TRACER("TQtPhone::on_pushButton_Hash_clicked()");
144
 
145
    mNumber += "#";
146
    ui->label_Number->setText(mNumber);
147
}
148
 
149
void TQtPhone::on_pushButton_Star_clicked()
150
{
151
    DECL_TRACER("TQtPhone::on_pushButton_Star_clicked()");
152
 
153
    mNumber += "*";
154
    ui->label_Number->setText(mNumber);
155
}
156
 
157
void TQtPhone::on_pushButton_Clear_clicked()
158
{
159
    DECL_TRACER("TQtPhone::on_pushButton_Clear_clicked()");
160
 
161
    mNumber.clear();
162
    ui->label_Number->setText(mNumber);
163
}
164
 
142 andreas 165
void TQtPhone::on_toolButton_Call_clicked()
139 andreas 166
{
141 andreas 167
    DECL_TRACER("TQtPhone::on_pushButton_Call_clicked()");
139 andreas 168
 
141 andreas 169
    if (!gPageManager)
170
        return;
171
 
172
    string ss = stateToString(mLastState);
173
    MSG_DEBUG("Current state: " << ss);
174
 
175
    if (mLastState == SIP_RINGING)
140 andreas 176
    {
177
        map<unsigned int, SIP_STATE_t>::iterator iter;
141 andreas 178
        int id = -1;
140 andreas 179
 
180
        for (iter = mSIPstate.begin(); iter != mSIPstate.end(); ++iter)
181
        {
141 andreas 182
            if (iter->second == SIP_RINGING)
140 andreas 183
            {
141 andreas 184
                id = iter->first;
185
                break;
140 andreas 186
            }
187
        }
141 andreas 188
#ifndef _NOSIP_
189
        MSG_DEBUG("Picking up line " << id);
190
        gPageManager->phonePickup(id);
191
#endif
192
        return;
140 andreas 193
    }
141 andreas 194
    else if (mLastState == SIP_CONNECTED || mLastState == SIP_TRYING)
140 andreas 195
    {
196
        map<unsigned int, SIP_STATE_t>::iterator iter;
197
        int id = -1;
198
 
199
        for (iter = mSIPstate.begin(); iter != mSIPstate.end(); ++iter)
200
        {
141 andreas 201
            if (iter->second == SIP_CONNECTED || iter->second == SIP_TRYING)
140 andreas 202
            {
203
                id = iter->first;
204
                break;
205
            }
206
        }
141 andreas 207
#ifndef _NOSIP_
208
        MSG_DEBUG("Hanging up line " << id);
209
        gPageManager->phoneHangup(id);
210
#endif
140 andreas 211
        return;
212
    }
213
    else if (mNumber.isEmpty())
141 andreas 214
    {
215
        MSG_DEBUG("No phone number to dial.");
140 andreas 216
        return;
141 andreas 217
    }
218
#ifndef _NOSIP_
140 andreas 219
    vector<string>cmds;
220
    cmds.push_back("CALL");
221
    cmds.push_back(mNumber.toStdString());
141 andreas 222
    gPageManager->actPHN(cmds);
140 andreas 223
    ui->toolButton_Call->setIcon(QIcon(":images/hangup.png"));
141 andreas 224
#endif
139 andreas 225
}
226
 
141 andreas 227
void TQtPhone::on_pushButton_Exit_clicked()
228
{
229
    DECL_TRACER("TQtPhone::on_pushButton_Exit_clicked()");
230
    #ifndef _NOSIP_
231
    if (mSIPstate.size() > 0)
232
    {
233
        map<unsigned int, SIP_STATE_t>::iterator iter;
234
 
235
        for (iter = mSIPstate.begin(); iter != mSIPstate.end(); ++iter)
236
        {
237
            if (iter->second == SIP_CONNECTED)
238
            {
239
                if (gPageManager)
240
                {
241
                    vector<string>cmds;
242
                    cmds.push_back("HANGUP");
243
                    cmds.push_back(std::to_string(iter->first));
244
                    gPageManager->sendPHN(cmds);
245
                }
246
            }
247
        }
248
    }
249
    #endif
250
    close();
251
}
252
 
139 andreas 253
void TQtPhone::doResize()
254
{
255
    DECL_TRACER("TQtPhone::doResize()");
256
 
257
    // The main dialog window
258
    QSize size = this->size();
259
    QRect rect = this->geometry();
260
    size.scale(scale(size.width()), scale(size.height()), Qt::KeepAspectRatio);
261
    this->resize(size);
262
    this->move(scale(rect.left()), scale(rect.top()));
263
    QWidget *parent = this->parentWidget();
264
 
265
    if (parent)
266
    {
267
        rect = parent->geometry();
268
        this->move(rect.center() - this->rect().center());
269
    }
270
 
271
    // Layout
272
    // Iterate through childs and resize them
273
    QObjectList childs = children();
274
    QList<QObject *>::Iterator iter;
275
 
276
    for (iter = childs.begin(); iter != childs.end(); ++iter)
277
    {
181 andreas 278
        QObject *obj = *iter;
279
        QString name = obj->objectName();
139 andreas 280
 
281
        if (name.startsWith("toolButton"))
282
            scaleObject(dynamic_cast<QToolButton *>(obj));
283
        else if (name.startsWith("pushButton"))
284
            scaleObject(dynamic_cast<QPushButton *>(obj));
285
        else if (name.startsWith("label"))
286
            scaleObject(dynamic_cast<QLabel *>(obj));
287
        else if (name.startsWith("line"))
142 andreas 288
            scaleObject(dynamic_cast<QFrame *>(obj));
139 andreas 289
    }
290
}
291
 
140 andreas 292
void TQtPhone::setPhoneNumber(const std::string& number)
293
{
294
    DECL_TRACER("TQtPhone::setPhoneNumber(const std::string& number)");
295
 
296
    ui->label_Number->setText(number.c_str());
297
}
298
 
299
void TQtPhone::setPhoneStatus(const std::string& msg)
300
{
301
    DECL_TRACER("TQtPhone::setPhoneStatus(const std::string& msg)");
302
 
303
    ui->label_Status->setText(msg.c_str());
304
}
305
 
306
void TQtPhone::setPhoneState(int state, int id)
307
{
308
    DECL_TRACER("TQtPhone::setPhoneState(int state)");
309
 
310
    if (id < 0 || id > 4)
311
    {
312
        MSG_ERROR("Invalid call ID " << id << "!");
313
        return;
314
    }
315
 
141 andreas 316
    string ss;
317
 
140 andreas 318
    if (state >= 0 && state <= SIP_ERROR)
319
    {
320
        mSIPstate.insert_or_assign(id, (SIP_STATE_t)state);
321
        mLastState = (SIP_STATE_t)state;
141 andreas 322
        ss = stateToString((SIP_STATE_t)state);
140 andreas 323
    }
324
    else
325
    {
326
        MSG_WARNING("Unknown state " << state << " for call id " << id << "!");
141 andreas 327
        ss = "??";
140 andreas 328
    }
329
 
141 andreas 330
    MSG_DEBUG("Setting line " << id << " to state " << ss);
331
 
140 andreas 332
    switch(state)
333
    {
334
        case SIP_TRYING:
141 andreas 335
            ui->label_Status->setText(QString("Line: %1 - TRYING").arg(id));
140 andreas 336
            ui->toolButton_Call->setIcon(QIcon(":images/hangup.png"));
337
        break;
338
 
339
        case SIP_CONNECTED:
141 andreas 340
            ui->label_Status->setText(QString("Line: %1 - CONNECTED").arg(id));
140 andreas 341
            ui->toolButton_Call->setIcon(QIcon(":images/hangup.png"));
342
        break;
343
 
344
        case SIP_DISCONNECTED:
141 andreas 345
            ui->label_Status->setText(QString("Line: %1 - DISCONNECTED").arg(id));
140 andreas 346
            ui->toolButton_Call->setIcon(QIcon(":images/pickup.png"));
141 andreas 347
            mNumber.clear();
348
            ui->label_Number->setText(mNumber);
140 andreas 349
        break;
350
 
351
        case SIP_REJECTED:
141 andreas 352
            ui->label_Status->setText(QString("Line: %1 - REJECTED").arg(id));
140 andreas 353
            ui->toolButton_Call->setIcon(QIcon(":images/pickup.png"));
354
        break;
355
 
356
        case SIP_RINGING:
141 andreas 357
            ui->label_Status->setText(QString("Line: %1 - RINGING").arg(id));
140 andreas 358
            ui->toolButton_Call->setIcon(QIcon(":images/pickup.png"));
141 andreas 359
            mNumber.clear();
360
            ui->label_Number->setText(mNumber);
140 andreas 361
        break;
362
 
363
        case SIP_ERROR:
141 andreas 364
            ui->label_Status->setText(QString("Line: %1 - ERROR").arg(id));
140 andreas 365
            ui->toolButton_Call->setIcon(QIcon(":images/pickup.png"));
141 andreas 366
            mNumber.clear();
367
            ui->label_Number->setText(mNumber);
140 andreas 368
        break;
369
 
370
        default:
371
            ui->toolButton_Call->setIcon(QIcon(":images/pickup.png"));
372
            ui->label_Status->setText("");
141 andreas 373
            mNumber.clear();
374
            ui->label_Number->setText(mNumber);
140 andreas 375
    }
376
}
377
 
139 andreas 378
template<typename T>
379
void TQtPhone::scaleObject(T *obj)
380
{
142 andreas 381
    DECL_TRACER("TQtPhone::scaleObject(T *obj): " + obj->objectName().toStdString());
139 andreas 382
 
383
    QSize size = obj->size();
384
    size.scale(scale(size.width()), scale(size.height()), Qt::KeepAspectRatio);
385
    obj->resize(size);
386
    QRect rect = obj->geometry();
387
    obj->move(scale(rect.left()), scale(rect.top()));
388
}
389
 
390
int TQtPhone::scale(int value)
391
{
392
    if (value <= 0 || mScaleFactor == 1.0)
393
        return value;
394
 
395
    return (int)((double)value * mScaleFactor);
396
}
141 andreas 397
 
398
string TQtPhone::stateToString(SIP_STATE_t state)
399
{
400
    DECL_TRACER("TQtPhone::stateToString(SIP_STATE_t state)");
401
 
402
    switch(state)
403
    {
404
        case SIP_CONNECTED:     return "CONNECTED";
405
        case SIP_DISCONNECTED:  return "DISCONNECTED";
406
        case SIP_ERROR:         return "ERROR";
407
        case SIP_HOLD:          return "HOLD";
408
        case SIP_IDLE:          return "IDLE";
409
        case SIP_NONE:          return "NONE";
410
        case SIP_REJECTED:      return "REJECTED";
411
        case SIP_RINGING:       return "RINGING";
412
        case SIP_TRYING:        return "TRYING";
413
    }
169 andreas 414
 
415
    return string();
141 andreas 416
}