1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2007-2012 Licq developers <licq-dev@googlegroups.com>
4  *
5  * Licq is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Licq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Licq; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include "timezoneedit.h"
21 
22 #include <QRegExp>
23 
24 #include <licq/contactlist/user.h>
25 
26 using namespace LicqQtGui;
27 /* TRANSLATOR LicqQtGui::TimeZoneEdit */
28 
TimeZoneEdit(QWidget * parent)29 TimeZoneEdit::TimeZoneEdit(QWidget* parent)
30   : QSpinBox(parent)
31 {
32   // Timezones range from -12 to +14 [wikipedia]
33   setMinimum(-13*3600); // Lowest value represents "Unknown"
34   setMaximum(14*3600);
35   setSingleStep(3600/2);
36 
37   // The world is round so let timezones wrap
38   setWrapping(true);
39 
40   // Plus and minus seems more fitting than up and down
41   setButtonSymbols(QSpinBox::PlusMinus);
42 
43   // Allow the value to be undefined as well. This will replace the lowest value (-24)
44   setSpecialValueText(tr("Unknown"));
45 }
46 
setData(int data)47 void TimeZoneEdit::setData(int data)
48 {
49   // The spinbox uses the lowest value to mark the undefined state but the constant is some other value so we need to change it
50   // For all defined values, the sign is inverted
51   setValue(data <= 12*3600 && data > -12*3600 ? data : minimum());
52 }
53 
data() const54 int TimeZoneEdit::data() const
55 {
56   int v = value();
57   if (v == minimum())
58     return Licq::User::TimezoneUnknown;
59   return v;
60 }
61 
validate(QString & input,int &) const62 QValidator::State TimeZoneEdit::validate(QString& input, int& /* pos */) const
63 {
64   // First check for the undefined value
65   if (input == specialValueText())
66     return QValidator::Acceptable;
67   if (specialValueText().startsWith(input))
68     return QValidator::Intermediate;
69 
70   // Check if this is a complete valid timezone
71   QRegExp rxValid("^GMT[\\+\\-](1[012]|\\d):[0-5]\\d$");
72   if (rxValid.indexIn(input) > -1)
73     return QValidator::Acceptable;
74 
75   // Check if this is anything close to a timezone
76   QRegExp rxPossible("^G?M?T?[\\+\\-]?\\d*:?\\d*$");
77   if (rxPossible.indexIn(input) > -1)
78     return QValidator::Intermediate;
79 
80   return QValidator::Invalid;
81 }
82 
textFromValue(int v) const83 QString TimeZoneEdit::textFromValue(int v) const
84 {
85   // The internal value in the spinbox is 30min intervals so convert it to something more readable
86   return QString("GMT%1%2:%3").arg(v < 0 ? "-" : "+").arg(abs(v/3600)).arg((abs(v / 60) % 60), 2, 10, QChar('0'));
87 }
88 
valueFromText(const QString & text) const89 int TimeZoneEdit::valueFromText(const QString& text) const
90 {
91   // The user entered something so now we must try and convert it back to the internal int
92   QRegExp rx("^GMT(\\+|-)(1[012]|\\d):([0-5]\\d)$");
93   if (rx.indexIn(text) == -1)
94     return minimum();
95 
96   int ret = rx.cap(2).toInt() * 3600 + rx.cap(3).toInt() * 60;
97   if (rx.cap(1) == "-")
98     ret = -ret;
99   return ret;
100 }
101