1 /***************************************************************************
2 * Copyright (C) 2003 by S�astien Laot *
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. *
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. *
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 ***************************************************************************/
23 #include <Q3TextStream>
25 #include <kmessagebox.h>
27 #include "basketfactory.h"
31 #include "note.h" // For balanced column width computation
36 // TODO: Don't create a basket with a name that already exists!
38 QString
BasketFactory::newFolderName()
44 for (int i
= 1; ; ++i
) {
45 folderName
= "basket" + QString::number(i
) + "/";
46 fullPath
= Global::basketsFolder() + folderName
;
48 if ( ! dir
.exists() ) // OK : The folder do not yet exists :
49 break; // We've found one !
55 QString
BasketFactory::unpackTemplate(const QString
&templateName
)
57 // Find a name for a new folder and create it:
58 QString folderName
= newFolderName();
59 QString fullPath
= Global::basketsFolder() + folderName
;
61 if (!dir
.mkdir(fullPath
)) {
62 KMessageBox::error(/*parent=*/0, i18n("Sorry, but the folder creation for this new basket has failed."), i18n("Basket Creation Failed"));
66 // Unpack the template file to that folder:
67 // TODO: REALLY unpack (this hand-creation is temporary, or it could be used in case the template can't be found)
68 QFile
file(fullPath
+ "/.basket");
69 if (file
.open(QIODevice::WriteOnly
)) {
70 Q3TextStream
stream(&file
);
71 stream
.setEncoding(Q3TextStream::UnicodeUTF8
);
72 int nbColumns
= (templateName
== "mindmap" || templateName
== "free" ? 0 : templateName
.left(1).toInt());
73 Basket
*currentBasket
= Global::bnpView
->currentBasket();
74 int columnWidth
= (currentBasket
&& nbColumns
> 0 ? (currentBasket
->visibleWidth() - (nbColumns
-1)*Note::RESIZER_WIDTH
) / nbColumns
: 0);
75 stream
<< QString( "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
79 " <disposition mindMap=\"%1\" columnCount=\"%2\" free=\"%3\" />\n"
81 " <notes>\n" ).arg( (templateName
== "mindmap" ? "true" : "false"),
82 QString::number(nbColumns
),
83 (templateName
== "free" || templateName
== "mindmap" ? "true" : "false") );
85 for (int i
= 0; i
< nbColumns
; ++i
)
86 stream
<< QString(" <group width=\"%1\"></group>\n").arg(columnWidth
);
87 stream
<< " </notes>\n"
92 KMessageBox::error(/*parent=*/0, i18n("Sorry, but the template copying for this new basket has failed."), i18n("Basket Creation Failed"));
97 void BasketFactory::newBasket(const QString
&icon
,
99 const QString
&backgroundImage
,
100 const QColor
&backgroundColor
,
101 const QColor
&textColor
,
102 const QString
&templateName
,
105 // Unpack the templateName file to a new basket folder:
106 QString folderName
= unpackTemplate(templateName
);
107 if (folderName
.isEmpty())
110 // Read the properties, change those that should be customized and save the result:
111 QDomDocument
*document
= XMLWork::openFile("basket", Global::basketsFolder() + folderName
+ "/.basket");
113 KMessageBox::error(/*parent=*/0, i18n("Sorry, but the template customization for this new basket has failed."), i18n("Basket Creation Failed"));
116 QDomElement properties
= XMLWork::getElement(document
->documentElement(), "properties");
118 if (!icon
.isEmpty()) {
119 QDomElement iconElement
= XMLWork::getElement(properties
, "icon");
120 if (!iconElement
.tagName().isEmpty()) // If there is already an icon, remove it since we will add our own value below
121 iconElement
.removeChild(iconElement
.firstChild());
122 XMLWork::addElement(*document
, properties
, "icon", icon
);
125 if (!name
.isEmpty()) {
126 QDomElement nameElement
= XMLWork::getElement(properties
, "name");
127 if (!nameElement
.tagName().isEmpty()) // If there is already a name, remove it since we will add our own value below
128 nameElement
.removeChild(nameElement
.firstChild());
129 XMLWork::addElement(*document
, properties
, "name", name
);
132 if (backgroundColor
.isValid()) {
133 QDomElement appearanceElement
= XMLWork::getElement(properties
, "appearance");
134 if (appearanceElement
.tagName().isEmpty()) { // If there is not already an appearance tag, add it since we will access it below
135 appearanceElement
= document
->createElement("appearance");
136 properties
.appendChild(appearanceElement
);
138 appearanceElement
.setAttribute("backgroundColor", backgroundColor
.name());
141 if (!backgroundImage
.isEmpty()) {
142 QDomElement appearanceElement
= XMLWork::getElement(properties
, "appearance");
143 if (appearanceElement
.tagName().isEmpty()) { // If there is not already an appearance tag, add it since we will access it below
144 appearanceElement
= document
->createElement("appearance");
145 properties
.appendChild(appearanceElement
);
147 appearanceElement
.setAttribute("backgroundImage", backgroundImage
);
150 if (textColor
.isValid()) {
151 QDomElement appearanceElement
= XMLWork::getElement(properties
, "appearance");
152 if (appearanceElement
.tagName().isEmpty()) { // If there is not already an appearance tag, add it since we will access it below
153 appearanceElement
= document
->createElement("appearance");
154 properties
.appendChild(appearanceElement
);
156 appearanceElement
.setAttribute("textColor", textColor
.name());
159 // Load it in the parent basket (it will save the tree and switch to this new basket):
160 Global::bnpView
->loadNewBasket(folderName
, properties
, parent
);