2 #include <QSignalMapper>
7 #define PROCESSING "Processing..."
9 class QUssd : public QMainWindow {
13 QUssd(QString message, bool lock = false) {
16 replyLabel = new QTextEdit(message);
17 replyLabel->setMinimumWidth(400);
18 replyLabel->setMinimumHeight(200);
19 replyLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
20 replyLabel->setFrameStyle(QFrame::NoFrame);
21 QPalette pal = palette();
22 pal.setColor(QPalette::Base, Qt::transparent);
23 pal.setColor(QPalette::Text, pal.color(QPalette::ButtonText));
24 replyLabel->setPalette(pal);
25 replyLabel->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
27 numberLabel = new QLabel();
28 numberLabel->setMinimumWidth(400);
29 numberLabel->setMaximumHeight(100);
30 numberLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
32 indicationLayout = new QVBoxLayout;
33 indicationLayout->addWidget(numberLabel);
35 indicationLayout->addWidget(replyLabel);
37 padDel = new QPushButton("C");
38 connect(padDel, SIGNAL(clicked()), this, SLOT(del()));
39 padDel->setMaximumWidth(150);
40 padDel->setMaximumHeight(72);
41 padDel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
42 padSend = new QPushButton("SEND");
43 connect(padSend, SIGNAL(clicked()), this, SLOT(send()));
44 padSend->setMaximumWidth(300);
45 padSend->setMaximumHeight(72);
46 padSend->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
48 btnLayout = new QHBoxLayout;
49 btnLayout->addWidget(padSend);
50 btnLayout->addWidget(padDel);
52 numberPad = new QGridLayout();
53 numberPad->setSpacing(2);
55 QSignalMapper *signalMapper = new QSignalMapper(this);
57 numberPad->addWidget(createButton("1", signalMapper), 0, 0);
58 numberPad->addWidget(createButton("2", signalMapper), 0, 1);
59 numberPad->addWidget(createButton("3", signalMapper), 0, 2);
60 numberPad->addWidget(createButton("4", signalMapper), 1, 0);
61 numberPad->addWidget(createButton("5", signalMapper), 1, 1);
62 numberPad->addWidget(createButton("6", signalMapper), 1, 2);
63 numberPad->addWidget(createButton("7", signalMapper), 2, 0);
64 numberPad->addWidget(createButton("8", signalMapper), 2, 1);
65 numberPad->addWidget(createButton("9", signalMapper), 2, 2);
66 numberPad->addWidget(createButton("0", signalMapper), 3, 1);
67 numberPad->addWidget(createButton("#", signalMapper), 3, 2);
69 connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(appendToNumber(QString)));
71 padBtnStar = new QPushButton("*+");
72 connect(padBtnStar, SIGNAL(clicked()), this, SLOT(addStar()));
73 padBtnStar->setMaximumWidth(150);
74 padBtnStar->setMaximumHeight(72);
75 padBtnStar->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
76 numberPad->addWidget(padBtnStar, 3, 0);
78 centralWidget = new QWidget;
79 setCentralWidget(centralWidget);
80 mainLayout = new QGridLayout(centralWidget);
82 connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
90 USSDquery->write("exit\n");
91 USSDquery->closeWriteChannel();
92 USSDquery->waitForFinished();
98 QPushButton* createButton(QString caption, QSignalMapper *signalMapper) {
99 QPushButton *button = new QPushButton(caption);
100 signalMapper->setMapping(button, caption);
101 connect(button, SIGNAL(clicked()), signalMapper, SLOT (map()));
102 button->setMaximumWidth(150);
103 button->setMaximumHeight(72);
104 button->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
108 void initOrientation() {
109 QActionGroup *filterGroup = new QActionGroup(this);
110 filterGroup->setExclusive(true);
112 actPortrait = new QAction(tr("Portrait"), filterGroup);
113 actPortrait->setCheckable(true);
115 actLandscape = new QAction(tr("Landscape"), filterGroup);
116 actLandscape->setCheckable(true);
118 actAuto = new QAction(tr("Auto"), filterGroup);
119 actAuto->setCheckable(true);
120 actAuto->setChecked(true);
122 menuBar()->addActions(filterGroup->actions());
124 connect(actPortrait, SIGNAL(changed()), this, SLOT(setOrientation()));
125 connect(actLandscape, SIGNAL(changed()), this, SLOT(setOrientation()));
126 connect(actAuto, SIGNAL(changed()), this, SLOT(setOrientation()));
129 void restartUSSD( int, QProcess::ExitStatus) {
130 replyLabel->setText(QString("Error: ")+QString::fromUtf8(reply));
141 USSDquery->write(number.toUtf8().data());
142 USSDquery->write("\n");
143 number = tr(PROCESSING);
149 for (line = USSDquery->readLine(); !line.isEmpty(); line = USSDquery->readLine()) {
151 if (reply.endsWith("\n==MESSAGE END ==\n")) {
152 reply.chop(strlen("\n==MESSAGE END ==\n"));
153 if (!reply.isEmpty())
154 replyLabel->setText(QString::fromUtf8(reply));
163 void setOrientation() {
164 if (actLandscape->isChecked())
165 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
166 else if (actPortrait->isChecked())
167 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
169 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
172 void orientationChanged() {
173 QRect screenGeometry = QApplication::desktop()->screenGeometry();
174 mainLayout->removeItem(indicationLayout);
175 mainLayout->removeItem(numberPad);
176 mainLayout->removeItem(btnLayout);
178 if (screenGeometry.width() > screenGeometry.height()) {
179 mainLayout->addLayout(indicationLayout, 0, 0);
180 mainLayout->addLayout(btnLayout, 1, 0);
181 mainLayout->addLayout(numberPad, 0, 1, -1, 1);
183 mainLayout->addLayout(indicationLayout, 0, 0);
184 mainLayout->addLayout(numberPad, 1, 0);
185 mainLayout->addLayout(btnLayout, 2, 0);
189 void appendToNumber (QString str) {
190 if (number != tr(PROCESSING)) {
197 if (number != tr(PROCESSING)) {
198 // Do not change star if it was created more than 3 seconds before.
199 if (number[number.length()-1] == '*' && lastStar.elapsed() < 3*1000) {
200 number[number.length()-1] = '+';
211 if (number != tr(PROCESSING)) {
218 void startUSSD (bool lock = false) {
219 QString program = "ussdquery.py";
220 QStringList arguments;
221 arguments << "interactive" << "-d" << "\n==MESSAGE END ==\n";
225 USSDquery = new QProcess();
226 USSDquery->setProcessChannelMode(QProcess::MergedChannels);
227 connect(USSDquery, SIGNAL(readyReadStandardOutput()), this, SLOT(readData()));
228 connect(USSDquery, SIGNAL(readyReadStandardError()), this, SLOT(readData()));
229 connect(USSDquery, SIGNAL(finished( int, QProcess::ExitStatus )), this, SLOT(restartUSSD( int, QProcess::ExitStatus )));
231 USSDquery->start(program, arguments);
235 numberLabel->setText(QString("<p style=\"font-size: 30pt;\" align=\"center\">")+number+"</p>");
238 QAction *actPortrait;
239 QAction *actLandscape;
242 QTextEdit *replyLabel;
245 QVBoxLayout *indicationLayout;
246 QHBoxLayout *btnLayout;
247 QGridLayout *numberPad;
248 QGridLayout *mainLayout;
249 QWidget* centralWidget;
251 QPushButton *padBtnStar;
254 QPushButton *padSend;
264 int main(int argc, char *argv[]) {
265 QApplication app(argc, argv);
270 message = message.fromUtf8(argv[1]);
271 if (argc >=3 && !strcmp(argv[2], "lock"))
274 // If no reply is expected just show notification box
275 if (!lock && !message.isEmpty()) {
276 QMessageBox box(QMessageBox::NoIcon, QString::fromUtf8(dgettext("rtcom-call-ui", "call_ti_message")), message, QMessageBox::Ok);
277 box.button(QMessageBox::Ok)->setText(QString::fromUtf8(dgettext("hildon-libs", "wdgt_bd_done")));
280 QUssd w(message, lock);