File indexing completed on 2024-04-28 16:21:24

0001 /* This file is part of the KDE project
0002    Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
0003    Copyright 2005 Raphael Langerhorst <raphael.langerhorst@kdemail.net>
0004    Copyright 2003 Philipp Müller <philipp.mueller@gmx.de>
0005    Copyright 1998, 1999 Torben Weis <weis@kde.org>
0006 
0007    This library is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU Library General Public
0009    License as published by the Free Software Foundation; either
0010    version 2 of the License, or (at your option) any later version.
0011 
0012    This library is distributed in the hope that it will be useful,
0013    but WITHOUT ANY WARRANTY; without even the implied warranty of
0014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015    Library General Public License for more details.
0016 
0017    You should have received a copy of the GNU Library General Public License
0018    along with this library; see the file COPYING.LIB.  If not, write to
0019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020    Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #include "HeaderFooter.h"
0024 
0025 #include "part/Doc.h" // FIXME detach from part
0026 #include "Sheet.h"
0027 #include "SheetPrint.h"
0028 #include "SheetsDebug.h"
0029 
0030 #include <KoDocumentInfo.h>
0031 
0032 #include <KLocalizedString>
0033 #include <kmessagebox.h>
0034 
0035 #include <QDate>
0036 #include <QPainter>
0037 #include <QTime>
0038 #include <QUrl>
0039 
0040 #include <pwd.h>
0041 #include <unistd.h>
0042 
0043 
0044 #define NO_MODIFICATION_POSSIBLE \
0045     do { \
0046         KMessageBox::error( 0, i18n ( "You cannot change a protected sheet" ) ); return; \
0047     } while(0)
0048 
0049 
0050 using namespace Calligra::Sheets;
0051 
0052 HeaderFooter::HeaderFooter(Sheet *sheet)
0053         : m_pSheet(sheet)
0054 {
0055 }
0056 
0057 HeaderFooter::~HeaderFooter()
0058 {
0059 }
0060 
0061 void HeaderFooter::replaceHeadFootLineMacro(QString &_text, const QString &_search, const QString &_replace) const
0062 {
0063     if (_search != _replace)
0064         _text.replace(QString('<' + _search + '>'), '<' + _replace + '>');
0065 }
0066 
0067 QString HeaderFooter::localizeHeadFootLine(const QString &_text) const
0068 {
0069     QString tmp = _text;
0070 
0071     /*
0072       i18n:
0073       Please use the same words (even upper/lower case) as in
0074       KoPageLayoutDia.cc function setupTab2(), without the brackets "<" and ">"
0075     */
0076     replaceHeadFootLineMacro(tmp, "page",   i18n("page"));
0077     replaceHeadFootLineMacro(tmp, "pages",  i18n("pages"));
0078     replaceHeadFootLineMacro(tmp, "file",   i18n("file"));
0079     replaceHeadFootLineMacro(tmp, "name",   i18n("name"));
0080     replaceHeadFootLineMacro(tmp, "time",   i18n("time"));
0081     replaceHeadFootLineMacro(tmp, "date",   i18n("date"));
0082     replaceHeadFootLineMacro(tmp, "author", i18n("author"));
0083     replaceHeadFootLineMacro(tmp, "email",  i18n("email"));
0084     replaceHeadFootLineMacro(tmp, "org",    i18n("org"));
0085     replaceHeadFootLineMacro(tmp, "sheet",  i18n("sheet"));
0086 
0087     return tmp;
0088 }
0089 
0090 QString HeaderFooter::delocalizeHeadFootLine(const QString &_text) const
0091 {
0092     QString tmp = _text;
0093 
0094     /*
0095       i18n:
0096       Please use the same words (even upper/lower case) as in
0097       KoPageLayoutDia.cc function setupTab2(), without the brackets "<" and ">"
0098     */
0099     replaceHeadFootLineMacro(tmp, i18n("page"),   "page");
0100     replaceHeadFootLineMacro(tmp, i18n("pages"),  "pages");
0101     replaceHeadFootLineMacro(tmp, i18n("file"),   "file");
0102     replaceHeadFootLineMacro(tmp, i18n("name"),   "name");
0103     replaceHeadFootLineMacro(tmp, i18n("time"),   "time");
0104     replaceHeadFootLineMacro(tmp, i18n("date"),   "date");
0105     replaceHeadFootLineMacro(tmp, i18n("author"), "author");
0106     replaceHeadFootLineMacro(tmp, i18n("email"),  "email");
0107     replaceHeadFootLineMacro(tmp, i18n("org"),    "org");
0108     replaceHeadFootLineMacro(tmp, i18n("sheet"),  "sheet");
0109 
0110     return tmp;
0111 }
0112 
0113 void HeaderFooter::setHeadFootLine(const QString &_headl, const QString &_headm, const QString &_headr,
0114                                    const QString &_footl, const QString &_footm, const QString &_footr)
0115 {
0116     if (m_pSheet->isProtected())
0117         NO_MODIFICATION_POSSIBLE;
0118 
0119     m_headLeft  = _headl;
0120     m_headRight = _headr;
0121     m_headMid   = _headm;
0122     m_footLeft  = _footl;
0123     m_footRight = _footr;
0124     m_footMid   = _footm;
0125     if (m_pSheet->doc()) m_pSheet->doc()->setModified(true);
0126 }
0127 
0128 QString HeaderFooter::completeHeading(const QString &_data, int _page, const QString &_sheet) const
0129 {
0130     QString page(QString::number(_page));
0131     QString pages(QString::number(m_pSheet->print()->pageCount()));
0132 
0133     QString pathFileName(m_pSheet->doc()->url().path());
0134     if (pathFileName.isNull())
0135         pathFileName = "";
0136 
0137     QString fileName(m_pSheet->doc()->url().fileName());
0138     if (fileName.isNull())
0139         fileName = "";
0140 
0141     QString t(QTime::currentTime().toString());
0142     QString d(QDate::currentDate().toString());
0143     QString ta;
0144     if (!_sheet.isEmpty())
0145         ta = _sheet;
0146 
0147     KoDocumentInfo* info = m_pSheet->doc()->documentInfo();
0148     QString full_name;
0149     QString email_addr;
0150     QString organization;
0151     QString tmp;
0152     if (!info)
0153         warnSheets << "Author information not found in Document Info !";
0154     else {
0155         full_name = info->authorInfo("creator");
0156         email_addr = info->authorInfo("email");
0157         organization = info->authorInfo("company");
0158     }
0159 
0160     char hostname[80];
0161     struct passwd *p;
0162 
0163     p = getpwuid(getuid());
0164     gethostname(hostname, sizeof(hostname));
0165 
0166 #ifndef Q_OS_ANDROID
0167     if (full_name.isEmpty())
0168         full_name = p->pw_gecos;
0169 #endif
0170 
0171     if (email_addr.isEmpty())
0172         email_addr = QString("%1@%2").arg(p->pw_name).arg(hostname);
0173 
0174     tmp = _data;
0175     int pos = 0;
0176     while ((pos = tmp.indexOf("<page>", pos)) != -1)
0177         tmp.replace(pos, 6, page);
0178     pos = 0;
0179     while ((pos = tmp.indexOf("<pages>", pos)) != -1)
0180         tmp.replace(pos, 7, pages);
0181     pos = 0;
0182     while ((pos = tmp.indexOf("<file>", pos)) != -1)
0183         tmp.replace(pos, 6, pathFileName);
0184     pos = 0;
0185     while ((pos = tmp.indexOf("<name>", pos)) != -1)
0186         tmp.replace(pos, 6, fileName);
0187     pos = 0;
0188     while ((pos = tmp.indexOf("<time>", pos)) != -1)
0189         tmp.replace(pos, 6, t);
0190     pos = 0;
0191     while ((pos = tmp.indexOf("<date>", pos)) != -1)
0192         tmp.replace(pos, 6, d);
0193     pos = 0;
0194     while ((pos = tmp.indexOf("<author>", pos)) != -1)
0195         tmp.replace(pos, 8, full_name);
0196     pos = 0;
0197     while ((pos = tmp.indexOf("<email>", pos)) != -1)
0198         tmp.replace(pos, 7, email_addr);
0199     pos = 0;
0200     while ((pos = tmp.indexOf("<org>", pos)) != -1)
0201         tmp.replace(pos, 5, organization);
0202     pos = 0;
0203     while ((pos = tmp.indexOf("<sheet>", pos)) != -1)
0204         tmp.replace(pos, 7, ta);
0205 
0206     return tmp;
0207 }