27 #include <kalarmcal/identities.h>
29 #include <kcmdlineargs.h>
33 static bool convInterval(
const QByteArray& timeParam, KARecurrence::Type&,
int& timeInterval,
bool allowMonthYear =
false);
35 void CommandOptions::setError(
const QString& error)
43 mEditActionSet(false),
53 mFlags(KAEvent::DEFAULT_FONT),
56 mArgs = KCmdLineArgs::parsedArgs();
58 if (mArgs->isSet(
"test-set-time"))
60 QString time = mArgs->getOption(
"test-set-time");
62 setErrorParameter(
"--test-set-time");
65 if (checkCommand(
"tray",
TRAY))
68 if (checkCommand(
"list",
LIST))
71 setErrorParameter(
"--list");
75 || checkCommand(
"edit",
EDIT))
80 mEventId =
EventId(mArgs->getOption(mCommandName));
82 mEventId = mArgs->getOption(mCommandName);
87 mTemplateName = mArgs->getOption(mCommandName);
89 if (checkCommand(
"file",
NEW))
92 mEditAction = KAEvent::FILE;
93 mEditActionSet =
true;
94 mText = mArgs->getOption(mCommandName);
96 if (checkCommand(
"exec-display",
NEW))
99 mEditAction = KAEvent::COMMAND;
100 mEditActionSet =
true;
101 mFlags |= KAEvent::DISPLAY_COMMAND;
102 mText = mArgs->getOption(mCommandName);
103 int n = mArgs->count();
104 for (
int i = 0; i < n; ++i)
106 mText += QLatin1Char(
' ');
107 mText += mArgs->arg(i);
110 if (checkCommand(
"exec",
NEW))
113 mEditAction = KAEvent::COMMAND;
114 mEditActionSet =
true;
115 mText = mArgs->getOption(mCommandName);
116 int n = mArgs->count();
117 for (
int i = 0; i < n; ++i)
119 mText += QLatin1Char(
' ');
120 mText += mArgs->arg(i);
123 if (checkCommand(
"mail",
NEW))
126 mEditAction = KAEvent::EMAIL;
127 mEditActionSet =
true;
132 if (!mEditActionSet || (mEditAction != KAEvent::COMMAND && mEditAction != KAEvent::FILE))
134 mEditAction = KAEvent::MESSAGE;
135 mEditActionSet =
true;
138 mText = mArgs->arg(0);
140 if (checkCommand(
"edit-new-command",
EDIT_NEW))
143 mEditAction = KAEvent::COMMAND;
144 mEditActionSet =
true;
149 mEditAction = KAEvent::EMAIL;
150 mEditActionSet =
true;
155 mEditAction = KAEvent::AUDIO;
156 mEditActionSet =
true;
158 if (mError.isEmpty() && mCommand ==
NONE)
162 if (checkCommand(
"play",
NEW) || checkCommand(
"play-repeat",
NEW))
165 mEditAction = KAEvent::AUDIO;
166 mEditActionSet =
true;
171 kDebug() <<
"Message";
173 mCommandName =
"message";
175 mEditAction = KAEvent::MESSAGE;
176 mEditActionSet =
true;
177 mText = mArgs->arg(0);
180 if (mEditActionSet && mEditAction == KAEvent::EMAIL)
182 if (mArgs->isSet(
"subject"))
183 mSubject = mArgs->getOption(
"subject");
184 if (mArgs->isSet(
"from-id"))
185 mFromID = Identities::identityUoid(mArgs->getOption(
"from-id"));
186 QStringList params = mArgs->getOptionList(
"mail");
187 for (
int i = 0, count = params.count(); i < count; ++i)
189 QString addr = params[i];
191 setError(i18nc(
"@info:shell",
"<icode>%1</icode>: invalid email address", QLatin1String(
"--mail")));
193 KCalCore::Person::Ptr person(
new KCalCore::Person(QString(), addr));
194 mAddressees += person;
196 mAddressees += KCal::Person(QString(), addr);
199 params = mArgs->getOptionList(
"attach");
200 for (
int i = 0, count = params.count(); i < count; ++i)
201 mAttachments += params[i];
203 mText = mArgs->arg(0);
205 if (mArgs->isSet(
"disable-all"))
208 setErrorIncompatible(
"--disable-all", mCommandName);
233 if (mArgs->isSet(
"disable"))
234 setErrorIncompatible(
"--disable", mCommandName);
239 if (mArgs->isSet(
"color"))
242 QString colourText = mArgs->getOption(
"color");
243 if (colourText[0] == QLatin1Char(
'0') && colourText[1].toLower() == QLatin1Char(
'x'))
244 colourText.replace(0, 2, QLatin1String(
"#"));
245 mBgColour.setNamedColor(colourText);
246 if (!mBgColour.isValid())
247 setErrorParameter(
"--color");
249 if (mArgs->isSet(
"colorfg"))
252 QString colourText = mArgs->getOption(
"colorfg");
253 if (colourText[0] == QLatin1Char(
'0') && colourText[1].toLower() == QLatin1Char(
'x'))
254 colourText.replace(0, 2, QLatin1String(
"#"));
255 mFgColour.setNamedColor(colourText);
256 if (!mFgColour.isValid())
257 setErrorParameter(
"--colorfg");
260 if (mArgs->isSet(
"time"))
262 QByteArray dateTime = mArgs->getOption(
"time").toLocal8Bit();
264 setErrorParameter(
"--time");
267 mAlarmTime = KDateTime::currentLocalDateTime();
269 bool haveRecurrence = mArgs->isSet(
"recurrence");
272 if (mArgs->isSet(
"login"))
273 setErrorIncompatible(
"--login",
"--recurrence");
274 else if (mArgs->isSet(
"until"))
275 setErrorIncompatible(
"--until",
"--recurrence");
276 QString rule = mArgs->getOption(
"recurrence");
277 mRecurrence =
new KARecurrence;
278 mRecurrence->set(rule);
280 if (mArgs->isSet(
"interval"))
285 if (mArgs->isSet(
"login"))
286 setErrorIncompatible(
"--login",
"--interval");
288 if (mArgs->isSet(
"repeat"))
290 count = mArgs->getOption(
"repeat").toInt(&ok);
291 if (!ok || !count || count < -1 || (count < 0 && haveRecurrence))
292 setErrorParameter(
"--repeat");
294 else if (haveRecurrence)
295 setErrorRequires(
"--interval",
"--repeat");
296 else if (mArgs->isSet(
"until"))
299 QByteArray dateTime = mArgs->getOption(
"until").toLocal8Bit();
301 if (mArgs->isSet(
"time"))
306 setErrorParameter(
"--until");
307 else if (mAlarmTime.isDateOnly() && !endTime.isDateOnly())
308 setError(i18nc(
"@info:shell",
"Invalid <icode>%1</icode> parameter for date-only alarm", QLatin1String(
"--until")));
309 if (!mAlarmTime.isDateOnly() && endTime.isDateOnly())
310 endTime.setTime(QTime(23,59,59));
311 if (endTime < mAlarmTime)
312 setError(i18nc(
"@info:shell",
"<icode>%1</icode> earlier than <icode>%2</icode>", QLatin1String(
"--until"), QLatin1String(
"--time")));
319 KARecurrence::Type recurType;
320 if (!
convInterval(mArgs->getOption(
"interval").toLocal8Bit(), recurType, interval, !haveRecurrence))
321 setErrorParameter(
"--interval");
322 else if (mAlarmTime.isDateOnly() && recurType == KARecurrence::MINUTELY)
323 setError(i18nc(
"@info:shell",
"Invalid <icode>%1</icode> parameter for date-only alarm", QLatin1String(
"--interval")));
330 int longestInterval = mRecurrence->longestInterval();
331 if (count * interval > longestInterval)
332 setError(i18nc(
"@info:shell",
"Invalid <icode>%1</icode> and <icode>%2</icode> parameters: repetition is longer than <icode>%3</icode> interval",
333 QLatin1String(
"--interval"), QLatin1String(
"--repeat"), QLatin1String(
"--recurrence")));
334 mRepeatCount = count;
335 mRepeatInterval = interval;
342 mRecurrence =
new KARecurrence;
343 mRecurrence->set(recurType, interval, count, mAlarmTime, endTime);
348 if (mArgs->isSet(
"repeat"))
349 setErrorRequires(
"--repeat",
"--interval");
350 else if (mArgs->isSet(
"until"))
351 setErrorRequires(
"--until",
"--interval");
354 bool audioRepeat = mArgs->isSet(
"play-repeat");
355 if (audioRepeat || mArgs->isSet(
"play"))
358 const char* opt = audioRepeat ?
"--play-repeat" :
"--play";
359 if (audioRepeat && mArgs->isSet(
"play"))
360 setErrorIncompatible(
"--play",
"--play-repeat");
361 if (mArgs->isSet(
"beep"))
362 setErrorIncompatible(
"--beep", opt);
363 else if (mArgs->isSet(
"speak"))
364 setErrorIncompatible(
"--speak", opt);
365 mAudioFile = mArgs->getOption(audioRepeat ?
"play-repeat" :
"play");
366 if (mArgs->isSet(
"volume"))
369 int volumepc = mArgs->getOption(
"volume").toInt(&ok);
370 if (!ok || volumepc < 0 || volumepc > 100)
371 setErrorParameter(
"--volume");
372 mAudioVolume =
static_cast<float>(volumepc) / 100;
375 else if (mArgs->isSet(
"volume"))
376 setErrorRequires(
"--volume",
"--play",
"--play-repeat");
377 if (mArgs->isSet(
"speak"))
379 if (mArgs->isSet(
"beep"))
380 setErrorIncompatible(
"--beep",
"--speak");
382 setError(i18nc(
"@info:shell",
"<icode>%1</icode> requires speech synthesis to be configured using Jovie", QLatin1String(
"--speak")));
384 bool onceOnly = mArgs->isSet(
"reminder-once");
385 if (mArgs->isSet(
"reminder") || onceOnly)
388 if (onceOnly && mArgs->isSet(
"reminder"))
389 setErrorIncompatible(
"--reminder",
"--reminder-once");
390 const char* opt = onceOnly ?
"--reminder-once" :
"--reminder";
391 KARecurrence::Type recurType;
392 QByteArray optval = mArgs->getOption(onceOnly ?
"reminder-once" :
"reminder").toLocal8Bit();
393 bool after = (optval[0] ==
'+');
395 optval = optval.right(1);
397 setErrorParameter(opt);
398 else if (recurType == KARecurrence::MINUTELY && mAlarmTime.isDateOnly())
399 setError(i18nc(
"@info:shell",
"Invalid <icode>%1</icode> parameter for date-only alarm", QLatin1String(opt)));
401 mReminderMinutes = -mReminderMinutes;
403 mFlags |= KAEvent::REMINDER_ONCE;
406 if (mArgs->isSet(
"late-cancel"))
408 KARecurrence::Type recurType;
409 bool ok =
convInterval(mArgs->getOption(
"late-cancel").toLocal8Bit(), recurType, mLateCancel);
411 setErrorParameter(
"--late-cancel");
413 else if (mArgs->isSet(
"auto-close"))
414 setErrorRequires(
"--auto-close",
"--late-cancel");
416 if (mArgs->isSet(
"ack-confirm"))
417 mFlags |= KAEvent::CONFIRM_ACK;
418 if (mArgs->isSet(
"auto-close"))
419 mFlags |= KAEvent::AUTO_CLOSE;
420 if (mArgs->isSet(
"beep"))
421 mFlags |= KAEvent::BEEP;
422 if (mArgs->isSet(
"speak"))
423 mFlags |= KAEvent::SPEAK;
424 if (mArgs->isSet(
"korganizer"))
425 mFlags |= KAEvent::COPY_KORGANIZER;
426 if (mArgs->isSet(
"disable"))
427 mFlags |= KAEvent::DISABLED;
429 mFlags |= KAEvent::REPEAT_SOUND;
430 if (mArgs->isSet(
"login"))
431 mFlags |= KAEvent::REPEAT_AT_LOGIN;
432 if (mArgs->isSet(
"bcc"))
433 mFlags |= KAEvent::EMAIL_BCC;
434 if (mAlarmTime.isDateOnly())
435 mFlags |= KAEvent::ANY_TIME;
440 if (!mError.isEmpty())
442 kDebug() <<
"Interactive";
443 if (mArgs->isSet(
"ack-confirm"))
444 mError += QLatin1String(
"--ack-confirm ");
445 if (mArgs->isSet(
"attach"))
446 mError += QLatin1String(
"--attach ");
447 if (mArgs->isSet(
"auto-close"))
448 mError += QLatin1String(
"--auto-close ");
449 if (mArgs->isSet(
"bcc"))
450 mError += QLatin1String(
"--bcc ");
451 if (mArgs->isSet(
"beep"))
452 mError += QLatin1String(
"--beep ");
453 if (mArgs->isSet(
"color"))
454 mError += QLatin1String(
"--color ");
455 if (mArgs->isSet(
"colorfg"))
456 mError += QLatin1String(
"--colorfg ");
457 if (mArgs->isSet(
"disable"))
458 mError += QLatin1String(
"--disable ");
459 if (mArgs->isSet(
"from-id"))
460 mError += QLatin1String(
"--from-id ");
461 if (mArgs->isSet(
"korganizer"))
462 mError += QLatin1String(
"--korganizer ");
463 if (mArgs->isSet(
"late-cancel"))
464 mError += QLatin1String(
"--late-cancel ");
465 if (mArgs->isSet(
"login"))
466 mError += QLatin1String(
"--login ");
467 if (mArgs->isSet(
"play"))
468 mError += QLatin1String(
"--play ");
469 if (mArgs->isSet(
"play-repeat"))
470 mError += QLatin1String(
"--play-repeat ");
471 if (mArgs->isSet(
"reminder"))
472 mError += QLatin1String(
"--reminder ");
473 if (mArgs->isSet(
"reminder-once"))
474 mError += QLatin1String(
"--reminder-once ");
475 if (mArgs->isSet(
"speak"))
476 mError += QLatin1String(
"--speak ");
477 if (mArgs->isSet(
"subject"))
478 mError += QLatin1String(
"--subject ");
479 if (mArgs->isSet(
"time"))
480 mError += QLatin1String(
"--time ");
481 if (mArgs->isSet(
"volume"))
482 mError += QLatin1String(
"--volume ");
483 if (!mError.isEmpty())
484 mError += i18nc(
"@info:shell",
": option(s) only valid with an appropriate action option or message");
492 if (!mError.isEmpty())
503 std::cerr << errmsg.toLocal8Bit().data()
504 << i18nc(
"@info:shell",
"\nUse --help to get a list of available command line options.\n").toLocal8Bit().data();
514 bool CommandOptions::checkCommand(
const QByteArray& command, Command code,
EditAlarmDlg::Type allowedEditType)
516 if (!mError.isEmpty()
517 || !mArgs->isSet(command))
522 setErrorIncompatible(mCommandName,
"--" + command);
523 kDebug().nospace() <<
" --" <<
command;
530 void CommandOptions::setErrorRequires(
const char* opt,
const char* opt2,
const char* opt3)
533 setError(i18nc(
"@info:shell",
"<icode>%1</icode> requires <icode>%2</icode>", QLatin1String(opt), QLatin1String(opt2)));
535 setError(i18nc(
"@info:shell",
"<icode>%1</icode> requires <icode>%2</icode> or <icode>%3</icode>", QLatin1String(opt), QLatin1String(opt2), QLatin1String(opt3)));
538 void CommandOptions::setErrorParameter(
const char* opt)
540 setError(i18nc(
"@info:shell",
"Invalid <icode>%1</icode> parameter", QLatin1String(opt)));
543 void CommandOptions::setErrorIncompatible(
const QByteArray& opt1,
const QByteArray& opt2)
545 QByteArray o1 = opt1;
546 if (!opt1.startsWith(
"--") && opt1 !=
"message")
548 QByteArray o2 = opt2;
549 if (!opt2.startsWith(
"--") && opt2 !=
"message")
551 setError(i18nc(
"@info:shell",
"<icode>%1</icode> incompatible with <icode>%2</icode>", QString::fromLatin1(o1), QString::fromLatin1(o2)));
556 if (mArgs->isSet(optName) && mCommand !=
NONE
558 setErrorIncompatible(optName, mCommandName);
567 static bool convInterval(
const QByteArray& timeParam, KARecurrence::Type& recurType,
int& timeInterval,
bool allowMonthYear)
569 QByteArray timeString = timeParam;
573 uint length = timeString.length();
574 switch (timeString[length - 1])
579 recurType = KARecurrence::ANNUAL_DATE;
580 timeString = timeString.left(length - 1);
583 recurType = KARecurrence::WEEKLY;
584 timeString = timeString.left(length - 1);
587 recurType = KARecurrence::DAILY;
588 timeString = timeString.left(length - 1);
592 int i = timeString.indexOf(
'H');
597 recurType = KARecurrence::MONTHLY_DAY;
598 timeString = timeString.left(length - 1);
602 recurType = KARecurrence::MINUTELY;
603 interval = timeString.left(i).toUInt(&ok) * 60;
604 timeString = timeString.mid(i + 1, length - i - 2);
609 recurType = KARecurrence::MINUTELY;
613 interval += timeString.toUInt(&ok);
619 case KARecurrence::WEEKLY:
622 case KARecurrence::DAILY:
629 timeInterval =
static_cast<int>(interval);
630 return ok && interval;
static void printError(const QString &errmsg)
static int checkAddress(QString &address)
the KAlarm application object
static bool convertTimeString(const QByteArray &timeString, KDateTime &dateTime, const KDateTime &defaultDt=KDateTime(), bool allowTZ=true)
static bool convInterval(const QByteArray &timeParam, KARecurrence::Type &, int &timeInterval, bool allowMonthYear=false)
Unique event identifier for Akonadi.
bool speechEnabled() const