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 200 201 202 203 204 205 206 207
|
/* This file is part of the KDE project
Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
Copyright 2005 Raphael Langerhorst <raphael.langerhorst@kdemail.net>
Copyright 2003 Philipp Müller <philipp.mueller@gmx.de>
Copyright 1998, 1999 Torben Weis <weis@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "HeaderFooter.h"
#include "part/Doc.h" // FIXME detach from part
#include "Sheet.h"
#include "SheetPrint.h"
#include "SheetsDebug.h"
#include <KoDocumentInfo.h>
#include <KLocalizedString>
#include <kmessagebox.h>
#include <QDate>
#include <QPainter>
#include <QTime>
#include <QUrl>
#include <pwd.h>
#include <unistd.h>
#define NO_MODIFICATION_POSSIBLE \
do { \
KMessageBox::error( 0, i18n ( "You cannot change a protected sheet" ) ); return; \
} while(0)
using namespace Calligra::Sheets;
HeaderFooter::HeaderFooter(Sheet *sheet)
: m_pSheet(sheet)
{
}
HeaderFooter::~HeaderFooter()
{
}
void HeaderFooter::replaceHeadFootLineMacro(QString &_text, const QString &_search, const QString &_replace) const
{
if (_search != _replace)
_text.replace(QString('<' + _search + '>'), '<' + _replace + '>');
}
QString HeaderFooter::localizeHeadFootLine(const QString &_text) const
{
QString tmp = _text;
/*
i18n:
Please use the same words (even upper/lower case) as in
KoPageLayoutDia.cc function setupTab2(), without the brackets "<" and ">"
*/
replaceHeadFootLineMacro(tmp, "page", i18n("page"));
replaceHeadFootLineMacro(tmp, "pages", i18n("pages"));
replaceHeadFootLineMacro(tmp, "file", i18n("file"));
replaceHeadFootLineMacro(tmp, "name", i18n("name"));
replaceHeadFootLineMacro(tmp, "time", i18n("time"));
replaceHeadFootLineMacro(tmp, "date", i18n("date"));
replaceHeadFootLineMacro(tmp, "author", i18n("author"));
replaceHeadFootLineMacro(tmp, "email", i18n("email"));
replaceHeadFootLineMacro(tmp, "org", i18n("org"));
replaceHeadFootLineMacro(tmp, "sheet", i18n("sheet"));
return tmp;
}
QString HeaderFooter::delocalizeHeadFootLine(const QString &_text) const
{
QString tmp = _text;
/*
i18n:
Please use the same words (even upper/lower case) as in
KoPageLayoutDia.cc function setupTab2(), without the brackets "<" and ">"
*/
replaceHeadFootLineMacro(tmp, i18n("page"), "page");
replaceHeadFootLineMacro(tmp, i18n("pages"), "pages");
replaceHeadFootLineMacro(tmp, i18n("file"), "file");
replaceHeadFootLineMacro(tmp, i18n("name"), "name");
replaceHeadFootLineMacro(tmp, i18n("time"), "time");
replaceHeadFootLineMacro(tmp, i18n("date"), "date");
replaceHeadFootLineMacro(tmp, i18n("author"), "author");
replaceHeadFootLineMacro(tmp, i18n("email"), "email");
replaceHeadFootLineMacro(tmp, i18n("org"), "org");
replaceHeadFootLineMacro(tmp, i18n("sheet"), "sheet");
return tmp;
}
void HeaderFooter::setHeadFootLine(const QString &_headl, const QString &_headm, const QString &_headr,
const QString &_footl, const QString &_footm, const QString &_footr)
{
if (m_pSheet->isProtected())
NO_MODIFICATION_POSSIBLE;
m_headLeft = _headl;
m_headRight = _headr;
m_headMid = _headm;
m_footLeft = _footl;
m_footRight = _footr;
m_footMid = _footm;
if (m_pSheet->doc()) m_pSheet->doc()->setModified(true);
}
QString HeaderFooter::completeHeading(const QString &_data, int _page, const QString &_sheet) const
{
QString page(QString::number(_page));
QString pages(QString::number(m_pSheet->print()->pageCount()));
QString pathFileName(m_pSheet->doc()->url().path());
if (pathFileName.isNull())
pathFileName = "";
QString fileName(m_pSheet->doc()->url().fileName());
if (fileName.isNull())
fileName = "";
QString t(QTime::currentTime().toString());
QString d(QDate::currentDate().toString());
QString ta;
if (!_sheet.isEmpty())
ta = _sheet;
KoDocumentInfo* info = m_pSheet->doc()->documentInfo();
QString full_name;
QString email_addr;
QString organization;
QString tmp;
if (!info)
warnSheets << "Author information not found in Document Info !";
else {
full_name = info->authorInfo("creator");
email_addr = info->authorInfo("email");
organization = info->authorInfo("company");
}
char hostname[80];
struct passwd *p;
p = getpwuid(getuid());
gethostname(hostname, sizeof(hostname));
#ifndef Q_OS_ANDROID
if (full_name.isEmpty())
full_name = p->pw_gecos;
#endif
if (email_addr.isEmpty())
email_addr = QString("%1@%2").arg(p->pw_name).arg(hostname);
tmp = _data;
int pos = 0;
while ((pos = tmp.indexOf("<page>", pos)) != -1)
tmp.replace(pos, 6, page);
pos = 0;
while ((pos = tmp.indexOf("<pages>", pos)) != -1)
tmp.replace(pos, 7, pages);
pos = 0;
while ((pos = tmp.indexOf("<file>", pos)) != -1)
tmp.replace(pos, 6, pathFileName);
pos = 0;
while ((pos = tmp.indexOf("<name>", pos)) != -1)
tmp.replace(pos, 6, fileName);
pos = 0;
while ((pos = tmp.indexOf("<time>", pos)) != -1)
tmp.replace(pos, 6, t);
pos = 0;
while ((pos = tmp.indexOf("<date>", pos)) != -1)
tmp.replace(pos, 6, d);
pos = 0;
while ((pos = tmp.indexOf("<author>", pos)) != -1)
tmp.replace(pos, 8, full_name);
pos = 0;
while ((pos = tmp.indexOf("<email>", pos)) != -1)
tmp.replace(pos, 7, email_addr);
pos = 0;
while ((pos = tmp.indexOf("<org>", pos)) != -1)
tmp.replace(pos, 5, organization);
pos = 0;
while ((pos = tmp.indexOf("<sheet>", pos)) != -1)
tmp.replace(pos, 7, ta);
return tmp;
}
|