Warning, file /office/calligra/libs/kundo2/kundo2magicstring.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  Copyright (c) 2014 Dmitry Kazakov <dimula73@gmail.com>
0003  *  Copyright (c) 2014 Alexander Potashev <aspotashev@gmail.com>
0004  *
0005  *  This library is free software; you can redistribute it and/or
0006  *  modify it under the terms of the GNU Library General Public
0007  *  License as published by the Free Software Foundation; either
0008  *  version 2 of the License, or (at your option) any later version.
0009  *
0010  *  This library is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  *  Library General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU Library General Public License
0016  *  along with this library; see the file COPYING.LIB.  If not, write to
0017  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  *  Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #ifndef KUNDO2MAGICSTRING_H
0022 #define KUNDO2MAGICSTRING_H
0023 
0024 #include <QString>
0025 #include <QDebug>
0026 
0027 #include <klocalizedstring.h>
0028 
0029 #include "kundo2_export.h"
0030 
0031 /**
0032  * \class KUndo2MagicString is a special wrapper for a string that is
0033  * going to passed to a KUndo2Command and be later shown in the undo
0034  * history and undo action in menu. The strings like that must have
0035  * (qtundo-format) context to let translators know that they are
0036  * allowed to use magic split in them.
0037  *
0038  * Magic split is used in some languages to split the message in the
0039  * undo history docker (which is either verb or <a
0040  * href="http://en.wikipedia.org/wiki/Nominative_case">noun in
0041  * nominative</a>) and the message in undo/redo actions (which is
0042  * usually a <a href="http://en.wikipedia.org/wiki/Accusative_case">noun
0043  * in accusative</a>). When the translator needs it he, splits two
0044  * translations with '\n' symbol and the magic string will recognize
0045  * it.
0046  *
0047  * \note KUndo2MagicString will never support concatenation operators,
0048  *       because in many languages you cannot combine words without
0049  *       knowing the proper case.
0050  */
0051 class KUNDO2_EXPORT KUndo2MagicString
0052 {
0053 public:
0054     /**
0055      * Construct an empty string. Note that you cannot create a
0056      * non-empy string without special functions, all the calls to which
0057      * are processed by xgettext.
0058      */
0059     KUndo2MagicString();
0060 
0061     /**
0062      * Fetch the main translated string. That is the one that goes to
0063      * undo history and resembles the action name in verb/nominative
0064      */
0065     QString toString() const;
0066 
0067     /**
0068      * Fetch the secondary string which will go to the undo/redo
0069      * action.  This is usually a noun in accusative. If the
0070      * translator didn't provide a secondary string, toString() and
0071      * toSecondaryString() return the same values.
0072      */
0073     QString toSecondaryString() const;
0074 
0075     /**
0076      * \return true if the contained string is empty
0077      */
0078     bool isEmpty() const;
0079 
0080     bool operator==(const KUndo2MagicString &rhs) const;
0081     bool operator!=(const KUndo2MagicString &rhs) const;
0082 
0083 private:
0084     /**
0085      * Construction of a magic string is allowed only with the means
0086      * of special macros which resemble their kde-wide counterparts
0087      */
0088     explicit KUndo2MagicString(const QString &text);
0089 
0090 
0091     friend KUndo2MagicString kundo2_noi18n(const QString &text);
0092     template <typename A1>
0093     friend KUndo2MagicString kundo2_noi18n(const char *text, const A1 &a1);
0094     template <typename A1, typename A2>
0095     friend KUndo2MagicString kundo2_noi18n(const char *text, const A1 &a1, const A2 &a2);
0096     template <typename A1, typename A2, typename A3>
0097     friend KUndo2MagicString kundo2_noi18n(const char *text, const A1 &a1, const A2 &a2, const A3 &a3);
0098     template <typename A1, typename A2, typename A3, typename A4>
0099     friend KUndo2MagicString kundo2_noi18n(const char *text, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4);
0100 
0101 
0102     friend KUndo2MagicString kundo2_i18n(const char *text);
0103     template <typename A1>
0104     friend KUndo2MagicString kundo2_i18n(const char *text, const A1 &a1);
0105     template <typename A1, typename A2>
0106     friend KUndo2MagicString kundo2_i18n(const char *text, const A1 &a1, const A2 &a2);
0107     template <typename A1, typename A2, typename A3>
0108     friend KUndo2MagicString kundo2_i18n(const char *text, const A1 &a1, const A2 &a2, const A3 &a3);
0109     template <typename A1, typename A2, typename A3, typename A4>
0110     friend KUndo2MagicString kundo2_i18n(const char *text, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4);
0111 
0112 
0113     friend KUndo2MagicString kundo2_i18nc(const char *ctxt, const char *text);
0114     template <typename A1>
0115     friend KUndo2MagicString kundo2_i18nc(const char *ctxt, const char *text, const A1 &a1);
0116     template <typename A1, typename A2>
0117     friend KUndo2MagicString kundo2_i18nc(const char *ctxt, const char *text, const A1 &a1, const A2 &a2);
0118     template <typename A1, typename A2, typename A3>
0119     friend KUndo2MagicString kundo2_i18nc(const char *ctxt, const char *text, const A1 &a1, const A2 &a2, const A3 &a3);
0120 
0121 
0122     template <typename A1>
0123     friend KUndo2MagicString kundo2_i18np(const char *sing, const char *plur, const A1 &a1);
0124     template <typename A1, typename A2>
0125     friend KUndo2MagicString kundo2_i18np(const char *sing, const char *plur, const A1 &a1, const A2 &a2);
0126     template <typename A1, typename A2, typename A3>
0127     friend KUndo2MagicString kundo2_i18np(const char *sing, const char *plur, const A1 &a1, const A2 &a2, const A3 &a3);
0128 
0129 
0130     template <typename A1>
0131     friend KUndo2MagicString kundo2_i18ncp(const char *ctxt, const char *sing, const char *plur, const A1 &a1);
0132     template <typename A1, typename A2>
0133     friend KUndo2MagicString kundo2_i18ncp(const char *ctxt, const char *sing, const char *plur, const A1 &a1, const A2 &a2);
0134     template <typename A1, typename A2, typename A3>
0135     friend KUndo2MagicString kundo2_i18ncp(const char *ctxt, const char *sing, const char *plur, const A1 &a1, const A2 &a2, const A3 &a3);
0136 
0137 private:
0138     QString m_text;
0139 };
0140 
0141 inline QDebug operator<<(QDebug dbg, const KUndo2MagicString &v)
0142 {
0143     if (v.toString() != v.toSecondaryString()) {
0144         dbg.nospace() << v.toString() << "(" << v.toSecondaryString() << ")";
0145     } else {
0146         dbg.nospace() << v.toString();
0147     }
0148 
0149     return dbg.space();
0150 }
0151 
0152 
0153 /**
0154  * This is a special wrapper to a string which tells explicitly
0155  * that we don't need a translation for a given string. It is used
0156  * either in testing or internal commands, which don't go to the
0157  * stack directly.
0158  */
0159 inline KUndo2MagicString kundo2_noi18n(const QString &text)
0160 {
0161     return KUndo2MagicString(text);
0162 }
0163 
0164 template <typename A1>
0165 inline KUndo2MagicString kundo2_noi18n(const char *text, const A1 &a1)
0166 {
0167     return KUndo2MagicString(QString(text).arg(a1));
0168 }
0169 
0170 template <typename A1, typename A2>
0171 inline KUndo2MagicString kundo2_noi18n(const char *text, const A1 &a1, const A2 &a2)
0172 {
0173     return KUndo2MagicString(QString(text).arg(a1).arg(a2));
0174 }
0175 
0176 template <typename A1, typename A2, typename A3>
0177 inline KUndo2MagicString kundo2_noi18n(const char *text, const A1 &a1, const A2 &a2, const A3 &a3)
0178 {
0179     return KUndo2MagicString(QString(text).arg(a1).arg(a2).arg(a3));
0180 }
0181 
0182 template <typename A1, typename A2, typename A3, typename A4>
0183 inline KUndo2MagicString kundo2_noi18n(const char *text, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4)
0184 {
0185     return KUndo2MagicString(QString(text).arg(a1).arg(a2).arg(a3).arg(a4));
0186 }
0187 
0188 /**
0189  * Same as ki18n, but is supposed to work with strings going to
0190  * undo stack
0191  */
0192 
0193 inline KUndo2MagicString kundo2_i18n(const char *text)
0194 {
0195     return KUndo2MagicString(i18nc("(qtundo-format)", text));
0196 }
0197 
0198 template <typename A1>
0199 inline KUndo2MagicString kundo2_i18n(const char *text, const A1 &a1)
0200 {
0201     return KUndo2MagicString(i18nc("(qtundo-format)", text, a1));
0202 }
0203 
0204 template <typename A1, typename A2>
0205 inline KUndo2MagicString kundo2_i18n(const char *text, const A1 &a1, const A2 &a2)
0206 {
0207     return KUndo2MagicString(i18nc("(qtundo-format)", text, a1, a2));
0208 }
0209 
0210 template <typename A1, typename A2, typename A3>
0211 inline KUndo2MagicString kundo2_i18n(const char *text, const A1 &a1, const A2 &a2, const A3 &a3)
0212 {
0213     return KUndo2MagicString(i18nc("(qtundo-format)", text, a1, a2, a3));
0214 }
0215 
0216 template <typename A1, typename A2, typename A3, typename A4>
0217 inline KUndo2MagicString kundo2_i18n(const char *text, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4)
0218 {
0219     return KUndo2MagicString(i18nc("(qtundo-format)", text, a1, a2, a3, a4));
0220 }
0221 
0222 inline QString prependContext(const char *ctxt)
0223 {
0224     return QString("(qtundo-format) %1").arg(ctxt);
0225 }
0226 
0227 /**
0228  * Same as ki18nc, but is supposed to work with strings going to
0229  * undo stack
0230  */
0231 inline KUndo2MagicString kundo2_i18nc(const char *ctxt, const char *text)
0232 {
0233     return KUndo2MagicString(i18nc(prependContext(ctxt).toLatin1().data(), text));
0234 }
0235 
0236 template <typename A1>
0237 inline KUndo2MagicString kundo2_i18nc(const char *ctxt, const char *text, const A1 &a1)
0238 {
0239     return KUndo2MagicString(i18nc(prependContext(ctxt).toLatin1().data(), text, a1));
0240 }
0241 
0242 template <typename A1, typename A2>
0243 inline KUndo2MagicString kundo2_i18nc(const char *ctxt, const char *text, const A1 &a1, const A2 &a2)
0244 {
0245     return KUndo2MagicString(i18nc(prependContext(ctxt).toLatin1().data(), text, a1, a2));
0246 }
0247 
0248 template <typename A1, typename A2, typename A3>
0249 inline KUndo2MagicString kundo2_i18nc(const char *ctxt, const char *text, const A1 &a1, const A2 &a2, const A3 &a3)
0250 {
0251     return KUndo2MagicString(i18nc(prependContext(ctxt).toLatin1().data(), text, a1, a2, a3));
0252 }
0253 
0254 template <typename A1, typename A2, typename A3, typename A4>
0255 inline KUndo2MagicString kundo2_i18nc(const char *ctxt, const char *text, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4)
0256 {
0257     return KUndo2MagicString(i18nc(prependContext(ctxt).toLatin1().data(), text, a1, a2, a3, a4));
0258 }
0259 
0260 /**
0261  * Same as ki18np, but is supposed to work with strings going to
0262  * undo stack
0263  */
0264 
0265 template <typename A1>
0266 inline KUndo2MagicString kundo2_i18np(const char *sing, const char *plur, const A1 &a1)
0267 {
0268     return KUndo2MagicString(i18ncp("(qtundo-format)", sing, plur, a1));
0269 }
0270 
0271 template <typename A1, typename A2>
0272 inline KUndo2MagicString kundo2_i18np(const char *sing, const char *plur, const A1 &a1, const A2 &a2)
0273 {
0274     return i18ncp("(qtundo-format)", sing, plur, a1, a2);
0275 }
0276 
0277 template <typename A1, typename A2, typename A3>
0278 inline KUndo2MagicString kundo2_i18np(const char *sing, const char *plur, const A1 &a1, const A2 &a2, const A3 &a3)
0279 {
0280     return i18ncp("(qtundo-format)", sing, plur, a1, a2, a3);
0281 }
0282 
0283 template <typename A1, typename A2, typename A3, typename A4>
0284 inline KUndo2MagicString kundo2_i18np(const char *sing, const char *plur, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4)
0285 {
0286     return i18ncp("(qtundo-format)", sing, plur, a1, a2, a3, a4);
0287 }
0288 
0289 
0290 /**
0291  * Same as ki18ncp, but is supposed to work with strings going to
0292  * undo stack
0293  */
0294 template <typename A1>
0295 inline KUndo2MagicString kundo2_i18ncp(const char *ctxt, const char *sing, const char *plur, const A1 &a1)
0296 {
0297     return KUndo2MagicString(i18ncp(prependContext(ctxt).toLatin1().data(), sing, plur, a1));
0298 }
0299 
0300 template <typename A1, typename A2>
0301 inline KUndo2MagicString kundo2_i18ncp(const char *ctxt, const char *sing, const char *plur, const A1 &a1, const A2 &a2)
0302 {
0303     return i18ncp(prependContext(ctxt).toLatin1().data(), sing, plur, a1, a2);
0304 }
0305 
0306 template <typename A1, typename A2, typename A3>
0307 inline KUndo2MagicString kundo2_i18ncp(const char *ctxt, const char *sing, const char *plur, const A1 &a1, const A2 &a2, const A3 &a3)
0308 {
0309     return i18ncp(prependContext(ctxt).toLatin1().data(), sing, plur, a1, a2, a3);
0310 }
0311 
0312 template <typename A1, typename A2, typename A3, typename A4>
0313 inline KUndo2MagicString kundo2_i18ncp(const char *ctxt, const char *sing, const char *plur, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4)
0314 {
0315     return i18ncp(prependContext(ctxt).toLatin1().data(), sing, plur, a1, a2, a3, a4);
0316 }
0317 
0318 #endif /* KUNDO2MAGICSTRING_H */