File indexing completed on 2024-05-19 05:21:53

0001 /*
0002  * Copyright (C) 2007 by Thorsten Staerk <dev@staerk.de>
0003  * Copyright (C) 2019, 2021  Alexander Potashev <aspotashev@gmail.com>
0004  *
0005  *   This program is free software; you can redistribute it and/or modify
0006  *   it under the terms of the GNU General Public License as published by
0007  *   the Free Software Foundation; either version 2 of the License, or
0008  *   (at your option) any later version.
0009  *
0010  *   This program 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
0013  *   GNU General Public License for more details.
0014  *
0015  *   You should have received a copy of the GNU General Public License along
0016  *   with this program; if not, write to the
0017  *      Free Software Foundation, Inc.
0018  *      51 Franklin Street, Fifth Floor
0019  *      Boston, MA  02110-1301  USA.
0020  *
0021  */
0022 
0023 #include "ktimetrackerutility.h"
0024 
0025 #include <cmath>
0026 
0027 #include <QLocale>
0028 
0029 QString formatTime(double minutes, bool decimal)
0030 {
0031     QString time;
0032     if (decimal) {
0033         time = QStringLiteral("%1").arg(minutes / 60.0, 0, 'f', 2);
0034         time.replace(QChar::fromLatin1('.'), QLocale().decimalPoint());
0035     } else {
0036         const auto absMinutes = static_cast<qlonglong>(std::round(std::fabs(minutes)));
0037         time = QStringLiteral("%1%2:%3")
0038                    .arg(minutes < 0 ? QString(QLocale().negativeSign()) : QStringLiteral())
0039                    .arg(absMinutes / 60)
0040                    .arg(absMinutes % 60, 2, 10, QLatin1Char('0'));
0041     }
0042 
0043     return time;
0044 }
0045 
0046 QString getCustomProperty(const KCalendarCore::Incidence::Ptr &incident, const QString &name)
0047 {
0048     static const QByteArray eventAppName = QByteArray("ktimetracker");
0049     static const QByteArray eventAppNameOld = QByteArray("karm");
0050     const QByteArray nameArray = name.toLatin1();
0051 
0052     // If a KDE-karm-* exists and not KDE-ktimetracker-*, change this.
0053     if (incident->customProperty(eventAppName, nameArray).isNull()
0054         && !incident->customProperty(eventAppNameOld, nameArray).isNull()) {
0055         incident->setCustomProperty(eventAppName, nameArray, incident->customProperty(eventAppNameOld, nameArray));
0056     }
0057 
0058     return incident->customProperty(eventAppName, nameArray);
0059 }