source: qutecom-2.2/wengophone/src/presentation/qt/chat/QtChatTheme.cpp @ 581:fd80f7940877

Last change on this file since 581:fd80f7940877 was 581:fd80f7940877, checked in by laurent@…, 3 years ago

test theme dir before load

File size: 4.0 KB
Line 
1/*
2 * WengoPhone, a voice over Internet phone
3 * Copyright (C) 2004-2007  Wengo
4 *
5 * This program 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 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19#include "QtChatTheme.h"
20
21#include <model/config/ConfigManager.h>
22#include <model/config/Config.h>
23
24#include <QtCore/QDir>
25#include <QtCore/QFile>
26#include <QtCore/QFileInfo>
27#include <QtCore/QStringList>
28
29#include <util/Logger.h>
30
31static const char KEYWORD_CHAR = '%';
32
33QString QtChatTheme::getFooter() const {
34        return _footer;
35}
36
37QString QtChatTheme::getStatusMessage() const {
38        return _statusMessage;
39}
40
41QString QtChatTheme::getContent(Direction direction, Position position) const {
42        QString html;
43        if (direction == Incoming && position == First) {
44                html = _incomingContent;
45        } else if (direction == Outgoing && position == First) {
46                html = _outgoingContent;
47        } else if (direction == Incoming && position == Next) {
48                html = _incomingNextContent;
49        } else if (direction == Outgoing && position == Next) {
50                html = _outgoingNextContent;
51        } else {
52                LOG_FATAL("Unknown combination of direction and position");
53        }
54
55        return html;
56}
57
58QString QtChatTheme::getStyleSheet() const {
59        return _styleSheet;
60}
61
62static QString loadFile(const QString& path) {
63        QFile file(path);
64        file.open(QIODevice::ReadOnly);
65        QString data = QString::fromUtf8(file.readAll());
66        return data.trimmed();
67}
68
69static QString themeBaseDir() {
70        static QString baseDir;
71        if (baseDir.isEmpty()) {
72                Config & config = ConfigManager::getInstance().getCurrentConfig();
73                baseDir = QString::fromUtf8(config.getResourcesDir().c_str());
74
75#if defined(OS_WINDOWS)
76                baseDir = baseDir.replace("\\","/");
77#endif
78                baseDir += "/chat/";
79        }
80        return baseDir;
81}
82
83QString QtChatTheme::getCurrentThemePath()
84{
85        return _currentThemePath;
86}
87
88void QtChatTheme::load(const QString & themeDir) {
89        QStringList themeList = getThemeList();
90       
91        if(themeList.contains(themeDir))
92                _currentThemePath = themeBaseDir() + themeDir + "/";
93        else if(themeList.count())
94                _currentThemePath = themeBaseDir() + themeList.at(0) + "/";
95
96        _incomingContent = loadFile(_currentThemePath + "Contents/Resources/Incoming/Content.html");
97        _incomingNextContent = loadFile(_currentThemePath + "Contents/Resources/Incoming/NextContent.html");
98        _outgoingContent = loadFile(_currentThemePath + "Contents/Resources/Outgoing/Content.html");
99        _outgoingNextContent = loadFile(_currentThemePath + "Contents/Resources/Outgoing/NextContent.html");
100        _footer = loadFile(_currentThemePath + "Contents/Resources/Footer.html");
101        _statusMessage = loadFile(_currentThemePath + "Contents/Resources/Status.html");
102        _styleSheet = loadFile(_currentThemePath + "Contents/Resources/main.css");
103}
104
105void QtChatTheme::setKeywordValue(QString & html, const QString & keyword, const QString & value) {
106        html.replace(KEYWORD_CHAR + keyword + KEYWORD_CHAR, value);
107}
108
109QStringList QtChatTheme::getThemeList() {
110        QDir dir(themeBaseDir());
111
112        QStringList list;
113        Q_FOREACH(QFileInfo info, dir.entryInfoList()) {
114                if (info.isDir()) {
115                        QString name = info.fileName();
116                        if (name[0] != '.') {
117                                list << name;
118                        }
119                }
120        }
121
122        return list;
123}
124
125QStringList QtChatTheme::getThemeListVariant(const QString & theme) {
126        QDir dir(themeBaseDir() +theme+ "/Contents/Resources/Variants");
127       
128        QStringList result;
129        QStringList filters;
130        filters << "*.css";
131       
132        dir.setNameFilters(filters);
133        QFileInfoList infoList = dir.entryInfoList();
134        for(int i = 0 ; i < infoList.count() ; i ++)
135                result.append(infoList.at(i).baseName());
136                                       
137        return result;
138}
Note: See TracBrowser for help on using the repository browser.