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)
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)
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;
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"));
187 for (
int i = 0, count = params.
count(); i < count; ++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");
247 setErrorParameter(
"--color");
249 if (mArgs->isSet(
"colorfg"))
252 QString colourText = mArgs->getOption(
"colorfg");
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, intervalOfType, !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")));
331 mRepeatInterval = KCalCore::Duration(intervalOfType * 60);
332 KCalCore::Duration longestInterval = mRecurrence->longestInterval();
333 if (mRepeatInterval * count > longestInterval)
334 setError(i18nc(
"@info:shell",
"Invalid <icode>%1</icode> and <icode>%2</icode> parameters: repetition is longer than <icode>%3</icode> interval",
336 mRepeatCount = count;
343 mRecurrence =
new KARecurrence;
344 mRecurrence->set(recurType, intervalOfType, count, mAlarmTime, endTime);
349 if (mArgs->isSet(
"repeat"))
350 setErrorRequires(
"--repeat",
"--interval");
351 else if (mArgs->isSet(
"until"))
352 setErrorRequires(
"--until",
"--interval");
355 bool audioRepeat = mArgs->isSet(
"play-repeat");
356 if (audioRepeat || mArgs->isSet(
"play"))
359 const char* opt = audioRepeat ?
"--play-repeat" :
"--play";
360 if (audioRepeat && mArgs->isSet(
"play"))
361 setErrorIncompatible(
"--play",
"--play-repeat");
362 if (mArgs->isSet(
"beep"))
363 setErrorIncompatible(
"--beep", opt);
364 else if (mArgs->isSet(
"speak"))
365 setErrorIncompatible(
"--speak", opt);
366 mAudioFile = mArgs->getOption(audioRepeat ?
"play-repeat" :
"play");
367 if (mArgs->isSet(
"volume"))
370 int volumepc = mArgs->getOption(
"volume").toInt(&ok);
371 if (!ok || volumepc < 0 || volumepc > 100)
372 setErrorParameter(
"--volume");
373 mAudioVolume =
static_cast<float>(volumepc) / 100;
376 else if (mArgs->isSet(
"volume"))
377 setErrorRequires(
"--volume",
"--play",
"--play-repeat");
378 if (mArgs->isSet(
"speak"))
380 if (mArgs->isSet(
"beep"))
381 setErrorIncompatible(
"--beep",
"--speak");
383 setError(i18nc(
"@info:shell",
"<icode>%1</icode> requires speech synthesis to be configured using Jovie",
QLatin1String(
"--speak")));
385 bool onceOnly = mArgs->isSet(
"reminder-once");
386 if (mArgs->isSet(
"reminder") || onceOnly)
389 if (onceOnly && mArgs->isSet(
"reminder"))
390 setErrorIncompatible(
"--reminder",
"--reminder-once");
391 const char* opt = onceOnly ?
"--reminder-once" :
"--reminder";
392 KARecurrence::Type recurType;
393 QByteArray optval = mArgs->getOption(onceOnly ?
"reminder-once" :
"reminder").toLocal8Bit();
394 bool after = (optval[0] ==
'+');
398 setErrorParameter(opt);
399 else if (recurType == KARecurrence::MINUTELY && mAlarmTime.isDateOnly())
400 setError(i18nc(
"@info:shell",
"Invalid <icode>%1</icode> parameter for date-only alarm",
QLatin1String(opt)));
402 mReminderMinutes = -mReminderMinutes;
404 mFlags |= KAEvent::REMINDER_ONCE;
407 if (mArgs->isSet(
"late-cancel"))
409 KARecurrence::Type recurType;
410 bool ok =
convInterval(mArgs->getOption(
"late-cancel").toLocal8Bit(), recurType, mLateCancel);
412 setErrorParameter(
"--late-cancel");
414 else if (mArgs->isSet(
"auto-close"))
415 setErrorRequires(
"--auto-close",
"--late-cancel");
417 if (mArgs->isSet(
"ack-confirm"))
418 mFlags |= KAEvent::CONFIRM_ACK;
419 if (mArgs->isSet(
"auto-close"))
420 mFlags |= KAEvent::AUTO_CLOSE;
421 if (mArgs->isSet(
"beep"))
422 mFlags |= KAEvent::BEEP;
423 if (mArgs->isSet(
"speak"))
424 mFlags |= KAEvent::SPEAK;
425 if (mArgs->isSet(
"korganizer"))
426 mFlags |= KAEvent::COPY_KORGANIZER;
427 if (mArgs->isSet(
"disable"))
428 mFlags |= KAEvent::DISABLED;
430 mFlags |= KAEvent::REPEAT_SOUND;
431 if (mArgs->isSet(
"login"))
432 mFlags |= KAEvent::REPEAT_AT_LOGIN;
433 if (mArgs->isSet(
"bcc"))
434 mFlags |= KAEvent::EMAIL_BCC;
435 if (mAlarmTime.isDateOnly())
436 mFlags |= KAEvent::ANY_TIME;
443 kDebug() <<
"Interactive";
444 if (mArgs->isSet(
"ack-confirm"))
446 if (mArgs->isSet(
"attach"))
448 if (mArgs->isSet(
"auto-close"))
450 if (mArgs->isSet(
"bcc"))
452 if (mArgs->isSet(
"beep"))
454 if (mArgs->isSet(
"color"))
456 if (mArgs->isSet(
"colorfg"))
458 if (mArgs->isSet(
"disable"))
460 if (mArgs->isSet(
"from-id"))
462 if (mArgs->isSet(
"korganizer"))
464 if (mArgs->isSet(
"late-cancel"))
466 if (mArgs->isSet(
"login"))
468 if (mArgs->isSet(
"play"))
470 if (mArgs->isSet(
"play-repeat"))
472 if (mArgs->isSet(
"reminder"))
474 if (mArgs->isSet(
"reminder-once"))
476 if (mArgs->isSet(
"speak"))
478 if (mArgs->isSet(
"subject"))
480 if (mArgs->isSet(
"time"))
482 if (mArgs->isSet(
"volume"))
485 mError += i18nc(
"@info:shell",
": option(s) only valid with an appropriate action option or message");
505 << i18nc(
"@info:shell",
"\nUse --help to get a list of available command line options.\n").toLocal8Bit().data();
518 || !mArgs->isSet(command))
523 setErrorIncompatible(mCommandName,
"--" + command);
524 kDebug().nospace() <<
" --" <<
command;
531 void CommandOptions::setErrorRequires(
const char* opt,
const char* opt2,
const char* opt3)
539 void CommandOptions::setErrorParameter(
const char* opt)
541 setError(i18nc(
"@info:shell",
"Invalid <icode>%1</icode> parameter",
QLatin1String(opt)));
547 if (!opt1.
startsWith(
"--") && opt1 !=
"message")
550 if (!opt2.
startsWith(
"--") && opt2 !=
"message")
557 if (mArgs->isSet(optName) && mCommand !=
NONE
559 setErrorIncompatible(optName, mCommandName);
568 static bool convInterval(
const QByteArray& timeParam, KARecurrence::Type& recurType,
int& timeInterval,
bool allowMonthYear)
574 uint length = timeString.
length();
575 switch (timeString[length - 1])
580 recurType = KARecurrence::ANNUAL_DATE;
581 timeString = timeString.
left(length - 1);
584 recurType = KARecurrence::WEEKLY;
585 timeString = timeString.
left(length - 1);
588 recurType = KARecurrence::DAILY;
589 timeString = timeString.
left(length - 1);
593 int i = timeString.
indexOf(
'H');
598 recurType = KARecurrence::MONTHLY_DAY;
599 timeString = timeString.
left(length - 1);
603 recurType = KARecurrence::MINUTELY;
604 interval = timeString.
left(i).
toUInt(&ok) * 60;
605 timeString = timeString.
mid(i + 1, length - i - 2);
610 recurType = KARecurrence::MINUTELY;
614 interval += timeString.
toUInt(&ok);
620 case KARecurrence::WEEKLY:
623 case KARecurrence::DAILY:
625 recurType = KARecurrence::MINUTELY;
631 timeInterval =
static_cast<int>(interval);
632 return ok && interval;
uint toUInt(bool *ok, int base) const
static void printError(const QString &errmsg)
static int checkAddress(QString &address)
bool startsWith(const QByteArray &ba) const
void setNamedColor(const QString &name)
the KAlarm application object
static bool convertTimeString(const QByteArray &timeString, KDateTime &dateTime, const KDateTime &defaultDt=KDateTime(), bool allowTZ=true)
int indexOf(char ch, int from) const
int count(const T &value) const
QByteArray & prepend(char ch)
static bool convInterval(const QByteArray &timeParam, KARecurrence::Type &, int &timeInterval, bool allowMonthYear=false)
QByteArray mid(int pos, int len) const
QByteArray toLocal8Bit() const
QString & replace(int position, int n, QChar after)
Unique event identifier for Akonadi.
QByteArray left(int len) const
QByteArray toLatin1() const
QString fromLatin1(const char *str, int size)
QByteArray & remove(int pos, int len)
bool speechEnabled() const