1 | /* This file is part of the KDE project |
2 | Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net> |
3 | Copyright 2005 Raphael Langerhorst <raphael.langerhorst@kdemail.net> |
4 | Copyright 2003 Philipp Müller <philipp.mueller@gmx.de> |
5 | Copyright 1998, 1999 Torben Weis <weis@kde.org> |
6 | |
7 | This library is free software; you can redistribute it and/or |
8 | modify it under the terms of the GNU Library General Public |
9 | License as published by the Free Software Foundation; either |
10 | version 2 of the License, or (at your option) any later version. |
11 | |
12 | This library is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | Library General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU Library General Public License |
18 | along with this library; see the file COPYING.LIB. If not, write to |
19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. |
21 | */ |
22 | |
23 | #include "HeaderFooter.h" |
24 | |
25 | #include "part/Doc.h" // FIXME detach from part |
26 | #include "Sheet.h" |
27 | #include "SheetPrint.h" |
28 | |
29 | #include <kdebug.h> |
30 | #include <klocale.h> |
31 | #include <kmessagebox.h> |
32 | #include <kurl.h> |
33 | |
34 | #include <KoDocumentInfo.h> |
35 | |
36 | #include <QDate> |
37 | #include <QPainter> |
38 | #include <QTime> |
39 | |
40 | #include <pwd.h> |
41 | #include <unistd.h> |
42 | |
43 | |
44 | #define NO_MODIFICATION_POSSIBLE \ |
45 | do { \ |
46 | KMessageBox::error( 0, i18n ( "You cannot change a protected sheet" ) ); return; \ |
47 | } while(0) |
48 | |
49 | |
50 | using namespace Calligra::Sheets; |
51 | |
52 | HeaderFooter::(Sheet *sheet) |
53 | : m_pSheet(sheet) |
54 | { |
55 | } |
56 | |
57 | HeaderFooter::() |
58 | { |
59 | } |
60 | |
61 | void HeaderFooter::(QString &_text, const QString &_search, const QString &_replace) const |
62 | { |
63 | if (_search != _replace) |
64 | _text.replace(QString('<' + _search + '>'), '<' + _replace + '>'); |
65 | } |
66 | |
67 | QString HeaderFooter::(const QString &_text) const |
68 | { |
69 | QString tmp = _text; |
70 | |
71 | /* |
72 | i18n: |
73 | Please use the same words (even upper/lower case) as in |
74 | KoPageLayoutDia.cc function setupTab2(), without the brackets "<" and ">" |
75 | */ |
76 | replaceHeadFootLineMacro(tmp, "page" , i18n("page" )); |
77 | replaceHeadFootLineMacro(tmp, "pages" , i18n("pages" )); |
78 | replaceHeadFootLineMacro(tmp, "file" , i18n("file" )); |
79 | replaceHeadFootLineMacro(tmp, "name" , i18n("name" )); |
80 | replaceHeadFootLineMacro(tmp, "time" , i18n("time" )); |
81 | replaceHeadFootLineMacro(tmp, "date" , i18n("date" )); |
82 | replaceHeadFootLineMacro(tmp, "author" , i18n("author" )); |
83 | replaceHeadFootLineMacro(tmp, "email" , i18n("email" )); |
84 | replaceHeadFootLineMacro(tmp, "org" , i18n("org" )); |
85 | replaceHeadFootLineMacro(tmp, "sheet" , i18n("sheet" )); |
86 | |
87 | return tmp; |
88 | } |
89 | |
90 | QString HeaderFooter::(const QString &_text) const |
91 | { |
92 | QString tmp = _text; |
93 | |
94 | /* |
95 | i18n: |
96 | Please use the same words (even upper/lower case) as in |
97 | KoPageLayoutDia.cc function setupTab2(), without the brackets "<" and ">" |
98 | */ |
99 | replaceHeadFootLineMacro(tmp, i18n("page" ), "page" ); |
100 | replaceHeadFootLineMacro(tmp, i18n("pages" ), "pages" ); |
101 | replaceHeadFootLineMacro(tmp, i18n("file" ), "file" ); |
102 | replaceHeadFootLineMacro(tmp, i18n("name" ), "name" ); |
103 | replaceHeadFootLineMacro(tmp, i18n("time" ), "time" ); |
104 | replaceHeadFootLineMacro(tmp, i18n("date" ), "date" ); |
105 | replaceHeadFootLineMacro(tmp, i18n("author" ), "author" ); |
106 | replaceHeadFootLineMacro(tmp, i18n("email" ), "email" ); |
107 | replaceHeadFootLineMacro(tmp, i18n("org" ), "org" ); |
108 | replaceHeadFootLineMacro(tmp, i18n("sheet" ), "sheet" ); |
109 | |
110 | return tmp; |
111 | } |
112 | |
113 | void HeaderFooter::(const QString &_headl, const QString &_headm, const QString &_headr, |
114 | const QString &, const QString &, const QString &) |
115 | { |
116 | if (m_pSheet->isProtected()) |
117 | NO_MODIFICATION_POSSIBLE; |
118 | |
119 | m_headLeft = _headl; |
120 | m_headRight = _headr; |
121 | m_headMid = _headm; |
122 | m_footLeft = _footl; |
123 | m_footRight = _footr; |
124 | m_footMid = _footm; |
125 | if (m_pSheet->doc()) m_pSheet->doc()->setModified(true); |
126 | } |
127 | |
128 | QString HeaderFooter::(const QString &_data, int _page, const QString &_sheet) const |
129 | { |
130 | QString page(QString::number(_page)); |
131 | QString pages(QString::number(m_pSheet->print()->pageCount())); |
132 | |
133 | QString pathFileName(m_pSheet->doc()->url().path()); |
134 | if (pathFileName.isNull()) |
135 | pathFileName = "" ; |
136 | |
137 | QString fileName(m_pSheet->doc()->url().fileName()); |
138 | if (fileName.isNull()) |
139 | fileName = "" ; |
140 | |
141 | QString t(QTime::currentTime().toString()); |
142 | QString d(QDate::currentDate().toString()); |
143 | QString ta; |
144 | if (!_sheet.isEmpty()) |
145 | ta = _sheet; |
146 | |
147 | KoDocumentInfo* info = m_pSheet->doc()->documentInfo(); |
148 | QString full_name; |
149 | QString email_addr; |
150 | QString organization; |
151 | QString tmp; |
152 | if (!info) |
153 | kWarning() << "Author information not found in Document Info !" ; |
154 | else { |
155 | full_name = info->authorInfo("creator" ); |
156 | email_addr = info->authorInfo("email" ); |
157 | organization = info->authorInfo("company" ); |
158 | } |
159 | |
160 | char hostname[80]; |
161 | struct passwd *p; |
162 | |
163 | p = getpwuid(getuid()); |
164 | gethostname(hostname, sizeof(hostname)); |
165 | |
166 | #ifndef Q_OS_ANDROID |
167 | if (full_name.isEmpty()) |
168 | full_name = p->pw_gecos; |
169 | #endif |
170 | |
171 | if (email_addr.isEmpty()) |
172 | email_addr = QString("%1@%2" ).arg(p->pw_name).arg(hostname); |
173 | |
174 | tmp = _data; |
175 | int pos = 0; |
176 | while ((pos = tmp.indexOf("<page>" , pos)) != -1) |
177 | tmp.replace(pos, 6, page); |
178 | pos = 0; |
179 | while ((pos = tmp.indexOf("<pages>" , pos)) != -1) |
180 | tmp.replace(pos, 7, pages); |
181 | pos = 0; |
182 | while ((pos = tmp.indexOf("<file>" , pos)) != -1) |
183 | tmp.replace(pos, 6, pathFileName); |
184 | pos = 0; |
185 | while ((pos = tmp.indexOf("<name>" , pos)) != -1) |
186 | tmp.replace(pos, 6, fileName); |
187 | pos = 0; |
188 | while ((pos = tmp.indexOf("<time>" , pos)) != -1) |
189 | tmp.replace(pos, 6, t); |
190 | pos = 0; |
191 | while ((pos = tmp.indexOf("<date>" , pos)) != -1) |
192 | tmp.replace(pos, 6, d); |
193 | pos = 0; |
194 | while ((pos = tmp.indexOf("<author>" , pos)) != -1) |
195 | tmp.replace(pos, 8, full_name); |
196 | pos = 0; |
197 | while ((pos = tmp.indexOf("<email>" , pos)) != -1) |
198 | tmp.replace(pos, 7, email_addr); |
199 | pos = 0; |
200 | while ((pos = tmp.indexOf("<org>" , pos)) != -1) |
201 | tmp.replace(pos, 5, organization); |
202 | pos = 0; |
203 | while ((pos = tmp.indexOf("<sheet>" , pos)) != -1) |
204 | tmp.replace(pos, 7, ta); |
205 | |
206 | return tmp; |
207 | } |
208 | |