File: firefoxfix.cpp

package info (click to toggle)
gtk-qt-engine 1%3A1.1%2Bsvn5-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 664 kB
  • ctags: 417
  • sloc: cpp: 2,113; ansic: 1,472; makefile: 16; sh: 10
file content (199 lines) | stat: -rw-r--r-- 6,436 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/***************************************************************************
 *   Copyright (C) 2008 by David Sansome                                   *
 *   me@davidsansome.com                                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "firefoxfix.h"

#include <kmessagebox.h>
#include <klocale.h>

#include <QDir>
#include <QSettings>
#include <QFile>
#include <QDebug>
#include <QScrollBar>
#include <QStyle>
#include <QApplication>
#include <QStyleOptionSlider>

bool FirefoxFix::s_scrollBarHasBack1;
bool FirefoxFix::s_scrollBarHasForward1;
bool FirefoxFix::s_scrollBarHasBack2;
bool FirefoxFix::s_scrollBarHasForward2;

void FirefoxFix::go()
{
	QStringList profiles;
	profiles << getProfiles(QDir::homePath() + "/.mozilla/firefox/");
	profiles << getProfiles(QDir::homePath() + "/.thunderbird/");
	
	if (profiles.count() == 0)
	{
		KMessageBox::error(0, i18n("No Mozilla profiles found"), i18n("Could not load Mozilla profiles"));
		return;
	}
	
	Q_FOREACH (QString profile, profiles)
		fixProfile(profile);
	
	KMessageBox::information(0, i18n("Your Mozilla profile was updated sucessfully.  You must close and restart all Firefox and Thunderbird windows for the changes to take effect"), i18n("Mozilla profile"));
}

QStringList FirefoxFix::getProfiles(const QString& basePath)
{
	QStringList ret;
	
	QString fileName = basePath + "/profiles.ini";
	if (QFile::exists(fileName))
	{
		QSettings settings(fileName, QSettings::IniFormat);
		
		Q_FOREACH (QString group, settings.childGroups())
		{
			if (!group.toLower().startsWith("profile"))
				continue;
			
			settings.beginGroup(group);
			QString path = settings.value("Path").toString();
			settings.endGroup();
			
			if (!path.startsWith("/"))
				path = basePath + path;
			
			ret << path;
		}
	}
	
	return ret;
}

void FirefoxFix::fixProfile(const QString& path)
{
	if (!QFile::exists(path + "/chrome"))
	{
		QDir dir(path);
		dir.mkdir("chrome");
	}
	
	QString data = scrollBarCSS();
	writeFirefoxCSS(path + "/chrome/userChrome.css", data);
	writeFirefoxCSS(path + "/chrome/userContent.css", data);
}

// Nicked from rcproperties.cpp
void FirefoxFix::findScrollBarButtons()
{
	static bool beenHereDoneThat = false;
	if (beenHereDoneThat)
		return;
	beenHereDoneThat = true;
	
	// I'm really quite ashamed of this function.
	// It loops over the ends of a scrollbar looking to see if each pixel is part of a button.
	
	QScrollBar* scrollBar = new QScrollBar(0);
	QStyle* qtStyle = QApplication::style();
	
	QStyleOptionSlider option;
	option.sliderValue = 1;
	option.sliderPosition = 1;
	option.rect = QRect(0, 0, 200, 25);
	option.state = QStyle::State_Horizontal;
	option.orientation = Qt::Horizontal;
	
	QRect rect = qtStyle->subControlRect(QStyle::CC_ScrollBar, &option, QStyle::SC_ScrollBarGroove, scrollBar);
	
	s_scrollBarHasBack1 = false;
	s_scrollBarHasForward1 = false;
	s_scrollBarHasBack2 = false;
	s_scrollBarHasForward2 = false;
	
	for (QPoint pos(0,7) ; pos.x()<rect.x() ; pos.setX(pos.x()+1))
	{
		QStyle::SubControl sc = qtStyle->hitTestComplexControl(QStyle::CC_ScrollBar, &option, pos, scrollBar);
		if (sc == QStyle::SC_ScrollBarAddLine) s_scrollBarHasForward1 = true;
		if (sc == QStyle::SC_ScrollBarSubLine) s_scrollBarHasBack1 = true;
	}
	
	for (QPoint pos(rect.x()+rect.width(),7) ; pos.x()<200 ; pos.setX(pos.x()+1))
	{
		QStyle::SubControl sc = qtStyle->hitTestComplexControl(QStyle::CC_ScrollBar, &option, pos, scrollBar);
		if (sc == QStyle::SC_ScrollBarAddLine) s_scrollBarHasForward2 = true;
		if (sc == QStyle::SC_ScrollBarSubLine) s_scrollBarHasBack2 = true;
	}
	
	delete scrollBar;
}

QString FirefoxFix::scrollBarCSS()
{
	findScrollBarButtons();
	
	QString upTop = (s_scrollBarHasBack1 ? "-moz-box" : "none");
	QString downTop = (s_scrollBarHasForward1 ? "-moz-box" : "none");
	QString upBottom = (s_scrollBarHasBack2 ? "-moz-box" : "none");
	QString downBottom = (s_scrollBarHasForward2 ? "-moz-box" : "none");
	
	QString data;
	data += "/* The following four lines were added by KDE */\n";
	data += "scrollbarbutton[sbattr=\"scrollbar-up-top\"] { display: " + upTop + " !important; }\n";
	data += "scrollbarbutton[sbattr=\"scrollbar-down-top\"] { display: " + downTop + " !important; }\n";
	data += "scrollbarbutton[sbattr=\"scrollbar-up-bottom\"] { display: " + upBottom + " !important; }\n";
	data += "scrollbarbutton[sbattr=\"scrollbar-down-bottom\"] { display: " + downBottom + " !important; }\n";
	
	return data;
}

void FirefoxFix::writeFirefoxCSS(const QString& path, const QString& data)
{
	QString fileData;
	QFile file(path);
	if (file.open(QIODevice::ReadOnly))
	{
		QTextStream stream(&file);
		for (;;)
		{
			QString line = stream.readLine();
			if (line.isNull())
				break;
			
			if ((line == "# The following four lines were added by KDE") ||
			    (line == "/* The following four lines were added by KDE */"))
			{
				for (int i=0 ; i<4 ; i++)
					stream.readLine();
				continue;
			}
			
			fileData += line + "\n";
		}
		file.close();
	}
	
	if (!file.open(QIODevice::WriteOnly))
	{
		KMessageBox::error(0, i18n("Could not write to %1").arg(path), i18n("Mozilla profile"));
		return;
	}
	QTextStream stream(&file);
	stream << fileData << data;
	file.close();
	
	return;
}