1 /*
2  * Colors.cpp
3  * Copyright (C) 2017  Belledonne Communications, Grenoble, France
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program 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 this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  *  Created on: June 18, 2017
20  *      Author: Ronan Abhamon
21  */
22 
23 #include <QMetaProperty>
24 
25 #include "../../../utils/Utils.hpp"
26 
27 #include "Colors.hpp"
28 
29 #define COLORS_SECTION "ui_colors"
30 
31 #if LINPHONE_FRIDAY
32 #include <QDate>
33 #endif // if LINPHONE_FRIDAY
34 
35 using namespace std;
36 
37 // =============================================================================
38 
39 #if LINPHONE_FRIDAY
40 
isLinphoneFriday()41   inline bool isLinphoneFriday () {
42     return QDate::currentDate().dayOfWeek() == 5;
43   }
44 
45 #endif // if LINPHONE_FRIDAY
46 
Colors(QObject * parent)47 Colors::Colors (QObject *parent) : QObject(parent) {
48   #if LINPHONE_FRIDAY
49     if (::isLinphoneFriday()) {
50       setProperty("i", QColor("#F48D8D"));
51       setProperty("s", QColor("#F58585"));
52       setProperty("t", QColor("#FFC5C5"));
53     }
54   #endif // if LINPHONE_FRIDAY
55 }
56 
useConfig(const shared_ptr<linphone::Config> & config)57 void Colors::useConfig (const shared_ptr<linphone::Config> &config) {
58   #if LINPHONE_FRIDAY
59     if (!::isLinphoneFriday())
60       overrideColors(config);
61   #else
62     overrideColors(config);
63   #endif // if LINPHONE_FRIDAY
64 }
65 
66 // -----------------------------------------------------------------------------
67 
overrideColors(const shared_ptr<linphone::Config> & config)68 void Colors::overrideColors (const shared_ptr<linphone::Config> &config) {
69   if (!config)
70     return;
71 
72   const QMetaObject *info = metaObject();
73 
74   for (int i = info->propertyOffset(); i < info->propertyCount(); ++i) {
75     const QMetaProperty metaProperty = info->property(i);
76     const string colorName = metaProperty.name();
77     const string colorValue = config->getString(COLORS_SECTION, colorName, "");
78 
79     if (!colorValue.empty())
80       setProperty(colorName.c_str(), QColor(::Utils::coreStringToAppString(colorValue)));
81   }
82 }
83 
getColorNames() const84 QStringList Colors::getColorNames () const {
85   static QStringList colorNames;
86   if (!colorNames.isEmpty())
87     return colorNames;
88 
89   const QMetaObject *info = metaObject();
90   for (int i = info->propertyOffset(); i < info->propertyCount(); ++i) {
91     const QMetaProperty metaProperty = info->property(i);
92     if (metaProperty.isWritable())
93       colorNames << QString::fromLatin1(metaProperty.name());
94   }
95 
96   return colorNames;
97 }
98