File indexing completed on 2024-06-23 05:45:42
0001 /* 0002 * Copyright 2020 Han Young <hanyoung@protonmail.com> 0003 * Copyright 2020-2021 Devin Lin <devin@kde.org> 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "utilmodel.h" 0009 #include "settingsmodel.h" 0010 0011 #include <QLocale> 0012 #include <QStandardPaths> 0013 #include <QTimeZone> 0014 #include <QUrl> 0015 0016 #include <KLocalizedString> 0017 0018 UtilModel *UtilModel::instance() 0019 { 0020 static UtilModel *singleton = new UtilModel; 0021 return singleton; 0022 } 0023 0024 UtilModel::UtilModel(QObject *parent) 0025 : QObject{parent} 0026 { 0027 connect(SettingsModel::instance(), &SettingsModel::timeFormatChanged, this, &UtilModel::use24HourTimeChanged); 0028 } 0029 0030 QString UtilModel::getDefaultAlarmFileLocation() 0031 { 0032 return QUrl::fromLocalFile(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("sounds/freedesktop/stereo/alarm-clock-elapsed.oga"))) 0033 .path(); 0034 } 0035 0036 QString UtilModel::getCurrentTimeZoneName() 0037 { 0038 return QString::fromStdString(QTimeZone::systemTimeZoneId().toStdString()); 0039 } 0040 0041 long long UtilModel::calculateNextRingTime(int hours, int minutes, int daysOfWeek, int snooze) 0042 { 0043 // get the time that the alarm will ring on the day 0044 QTime alarmTime = QTime(hours, minutes, 0).addSecs(snooze); 0045 0046 QDateTime date = QDateTime::currentDateTime(); 0047 0048 if (daysOfWeek == 0) { // alarm does not repeat (no days of the week are specified) 0049 if (alarmTime >= date.time()) { // alarm occurs later today 0050 return QDateTime(date.date(), alarmTime).toSecsSinceEpoch(); 0051 } else { // alarm occurs on the next day 0052 return QDateTime(date.date().addDays(1), alarmTime).toSecsSinceEpoch(); 0053 } 0054 } else { // repeat alarm 0055 bool first = true; 0056 0057 // keeping looping forward a single day until the day of week is accepted 0058 while (((daysOfWeek & (1 << (date.date().dayOfWeek() - 1))) == 0) // check day 0059 || (first && (alarmTime < date.time()))) // check time if the current day is accepted (keep looping forward if alarmTime has passed) 0060 { 0061 date = date.addDays(1); // go forward a day 0062 first = false; 0063 } 0064 0065 return QDateTime(date.date(), alarmTime).toSecsSinceEpoch(); 0066 } 0067 } 0068 0069 QString UtilModel::timeToRingFormatted(const long long ×tamp) 0070 { 0071 auto remaining = timestamp - QDateTime::currentSecsSinceEpoch(); 0072 int day = remaining / (24 * 3600); 0073 int hour = remaining / 3600 - day * 24; 0074 int minute = remaining / 60 - day * 24 * 60 - hour * 60; 0075 QString arg; 0076 if (day > 0) { 0077 arg += i18np("%1 day", "%1 days", day); 0078 } 0079 if (hour > 0) { 0080 if (day > 0 && minute > 0) { 0081 arg += i18n(", "); 0082 } else if (day > 0) { 0083 arg += i18n(" and "); 0084 } 0085 arg += i18np("%1 hour", "%1 hours", hour); 0086 } 0087 if (minute > 0) { 0088 if (day > 0 || hour > 0) { 0089 arg += i18n(" and "); 0090 } 0091 arg += i18np("%1 minute", "%1 minutes", minute); 0092 } 0093 0094 if (day <= 0 && hour <= 0 && minute <= 0) { 0095 return i18n("Alarm will ring in under a minute"); 0096 } else { 0097 return i18n("Alarm will ring in %1", arg); 0098 } 0099 } 0100 0101 QString UtilModel::timeFormat() const 0102 { 0103 return use24HourTime() ? QStringLiteral("hh:mm") : QStringLiteral("hh:mm ap"); 0104 } 0105 0106 bool UtilModel::use24HourTime() const 0107 { 0108 QString timeFormat = SettingsModel::instance()->timeFormat(); 0109 if (timeFormat == QStringLiteral("SystemDefault")) { 0110 return isLocale24HourTime(); 0111 } else { 0112 return timeFormat == QStringLiteral("24Hour"); 0113 } 0114 } 0115 0116 bool UtilModel::isLocale24HourTime() const 0117 { 0118 return QLocale::system().timeFormat(QLocale::ShortFormat).toLower().indexOf(QStringLiteral("ap")) == -1; 0119 }