]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinsettings.cpp
commited initial version of Dolphin
[dolphin.git] / src / dolphinsettings.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at), *
3 * Cvetoslav Ludmiloff and Patrice Tremblay *
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 *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include "dolphinsettings.h"
22
23 #include <assert.h>
24 #include <qdir.h>
25
26 #include <kapplication.h>
27 #include <kbookmark.h>
28 #include <kbookmarkmanager.h>
29 #include <kicontheme.h>
30 #include <klocale.h>
31 #include <kstandarddirs.h>
32
33 #include "dolphin.h"
34 #include "generalsettings.h"
35 #include "iconsmodesettings.h"
36 #include "previewsmodesettings.h"
37 #include "detailsmodesettings.h"
38 #include "sidebarsettings.h"
39
40 DolphinSettings& DolphinSettings::instance()
41 {
42 static DolphinSettings* instance = 0;
43 if (instance == 0) {
44 instance = new DolphinSettings();
45 }
46 return *instance;
47 }
48
49 KBookmark DolphinSettings::bookmark(int index) const
50 {
51 int i = 0;
52 KBookmarkGroup root = bookmarkManager()->root();
53 KBookmark bookmark = root.first();
54 while (!bookmark.isNull()) {
55 if (i == index) {
56 return bookmark;
57 }
58 ++i;
59 bookmark = root.next(bookmark);
60 }
61
62 return KBookmark();
63 }
64
65 KBookmarkManager* DolphinSettings::bookmarkManager() const
66 {
67 QString basePath = KGlobal::instance()->instanceName();
68 basePath.append("/bookmarks.xml");
69 const QString file = locateLocal("data", basePath);
70
71 return KBookmarkManager::managerForFile(file, false);
72 }
73
74 void DolphinSettings::save()
75 {
76 m_generalSettings->writeConfig();
77 m_iconsModeSettings->writeConfig();
78 m_previewsModeSettings->writeConfig();
79 m_detailsModeSettings->writeConfig();
80 m_sidebarSettings->writeConfig();
81
82 QString basePath = KGlobal::instance()->instanceName();
83 basePath.append("/bookmarks.xml");
84 const QString file = locateLocal( "data", basePath);
85
86 KBookmarkManager* manager = KBookmarkManager::managerForFile(file, false);
87 manager->save(false);
88 }
89
90 void DolphinSettings::calculateGridSize(int hint)
91 {
92 // TODO: remove in KDE4
93 const int previewSize = m_iconsModeSettings->previewSize();
94 const int iconSize = m_iconsModeSettings->iconSize();
95 const int maxSize = (previewSize > iconSize) ? previewSize : iconSize;
96 const Q3IconView::Arrangement arrangement = (m_iconsModeSettings->arrangement() == "LeftToRight") ?
97 Q3IconView::LeftToRight : Q3IconView::TopToBottom;
98
99 int gridWidth = 0;
100 int gridHeight = 0;
101 if (arrangement == Q3IconView::LeftToRight) {
102 int widthUnit = maxSize + (maxSize / 2);
103 if (widthUnit < KIcon::SizeLarge) {
104 widthUnit = KIcon::SizeLarge;
105 }
106
107 gridWidth = widthUnit + hint * KIcon::SizeLarge;
108
109 gridHeight = iconSize;
110 if (gridHeight <= KIcon::SizeMedium) {
111 gridHeight = gridHeight * 2;
112 }
113 else {
114 gridHeight += maxSize / 2;
115 }
116 }
117 else {
118 assert(arrangement == Q3IconView::TopToBottom);
119 gridWidth = maxSize + (hint + 1) * (8 * m_iconsModeSettings->fontSize());
120
121 // The height-setting is ignored yet by KFileIconView if the TopToBottom
122 // arrangement is active. Anyway write the setting to have a defined value.
123 gridHeight = maxSize;
124 }
125
126 m_iconsModeSettings->setGridWidth(gridWidth);
127 m_iconsModeSettings->setGridHeight(gridHeight);
128 }
129
130 int DolphinSettings::textWidthHint() const
131 {
132 // TODO: remove in KDE4
133 const int previewSize = m_iconsModeSettings->previewSize();
134 const int iconSize = m_iconsModeSettings->iconSize();
135 const Q3IconView::Arrangement arrangement = (m_iconsModeSettings->arrangement() == "LeftToRight") ?
136 Q3IconView::LeftToRight : Q3IconView::TopToBottom;
137
138 const int gridWidth = m_iconsModeSettings->gridWidth();
139
140 const int maxSize = (previewSize > iconSize) ? previewSize : iconSize;
141 int hint = 0;
142 if (arrangement == Q3IconView::LeftToRight) {
143 int widthUnit = maxSize + (maxSize / 2);
144 if (widthUnit < KIcon::SizeLarge) {
145 widthUnit = KIcon::SizeLarge;
146 }
147 hint = (gridWidth - widthUnit) / KIcon::SizeLarge;
148 }
149 else {
150 assert(arrangement == Q3IconView::TopToBottom);
151 hint = (gridWidth - maxSize) / (8 * m_iconsModeSettings->fontSize()) - 1;
152 if (hint > 2) {
153 hint = 2;
154 }
155 }
156 return hint;
157 }
158
159 DolphinSettings::DolphinSettings()
160 {
161 m_generalSettings = new GeneralSettings();
162 m_iconsModeSettings = new IconsModeSettings();
163 m_previewsModeSettings = new PreviewsModeSettings();
164 m_detailsModeSettings = new DetailsModeSettings();
165 m_sidebarSettings = new SidebarSettings();
166 }
167
168 DolphinSettings::~DolphinSettings()
169 {
170 delete m_generalSettings;
171 m_generalSettings = 0;
172
173 delete m_iconsModeSettings;
174 m_iconsModeSettings = 0;
175
176 delete m_previewsModeSettings;
177 m_previewsModeSettings = 0;
178
179 delete m_detailsModeSettings;
180 m_detailsModeSettings = 0;
181
182 delete m_sidebarSettings;
183 m_sidebarSettings = 0;
184 }