calendarwidgetplugin/src/contentlayouthandler.cpp
changeset 0 db1bf15cefff
child 1 f8e7eccf5f96
equal deleted inserted replaced
-1:000000000000 0:db1bf15cefff
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Homescreen calendar widget's content layout handler.
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <HbColorScheme>
       
    20 #include <QDateTime>
       
    21 #include <HbLabel>
       
    22 #include <HbDocumentLoader>
       
    23 #include <HbCore>
       
    24 #include <HbFrameDrawer>
       
    25 #include <HbFrameItem>
       
    26 #include <QTimer>
       
    27 #ifndef TEST
       
    28 #include <agendautil.h>
       
    29 #else
       
    30 #include "mockagendautil.h"
       
    31 #endif
       
    32 #include <agendaentry.h>
       
    33 #include <xqsettingsmanager.h>
       
    34 #include <xqsettingskey.h>
       
    35 #include <calnotification.h>
       
    36 #include <QGraphicsLayout>
       
    37 #include <QPointF>
       
    38 #include <xqaiwrequest.h>
       
    39 #include <xqappmgr.h>
       
    40 #include <QCoreApplication>
       
    41 
       
    42 // User includes
       
    43 #include "contentlayouthandler.h"
       
    44 #include "calendarwidgetdebug.h"
       
    45 #include "datetimeobserver.h"
       
    46 
       
    47 // Local constants
       
    48 namespace {
       
    49     const HbFrameDrawer::FrameType REMINDER_FRAME_TYPE = HbFrameDrawer::OnePiece;
       
    50     const char *CONTENT_CONTAINER = "contentContainer";
       
    51     const char *UPPER_LABEL_SHORT = "upperLabelShort";
       
    52     const char *UPPER_LABEL_LONG = "upperLabelLong";
       
    53     const char *MIDDLE_LABEL = "middleLabel";
       
    54     const char *LOWER_LABEL = "lowerLabel";
       
    55     const char *REMINDER_ICON_LABEL = "reminderLabel";
       
    56     const char *REMINDER_IMAGE_NAME = "qtg_small_reminder";
       
    57     const char *HIGHLIGHT_IMAGE_NAME = "qtg_fr_hsitems_pressed";
       
    58     //same color applies to upper and middle labels
       
    59     const char *UPPER_LABEL_COLOR = "qtc_hs_list_item_title_normal";
       
    60     const char *LOWER_LABEL_COLOR = "qtc_hs_list_item_content_normal";
       
    61     const char *HIGHLIGHT_LABEL_COLOR = "qtc_hs_list_item_pressed";
       
    62     // time constants
       
    63     const int KHalfHour = 30;
       
    64     const int KSecondInMillisec = 1000;
       
    65     const int KSecondsInMinute = 60;
       
    66     const int KToday = 1;
       
    67     const int KComingDays = 0;
       
    68     const int KDaysInWeek = 7;
       
    69     const int KHourMinuteSeparatorIndex = 1;
       
    70     const int KPassedEventTimeoutSecs = 1800; // Half hour
       
    71     
       
    72     //from DateIconLayoutHandler
       
    73     const HbFrameDrawer::FrameType DATE_ICON_BACKGROUND_FRAME_TYPE = HbFrameDrawer::OnePiece;
       
    74     const char *ICON_CONTAINER = "iconContainer";
       
    75     const char *DATE_ICON_LABEL = "iconLabel";
       
    76     const char *DATE_ICON_DAYNUMBER = "dayNumber";
       
    77     const char *DATE_ICON_MONTHLABEL = "month";
       
    78     const char *DATE_ICON_HIGHLIGHTLABEL = "iconHighlight";
       
    79     const char *NUMBER_LABEL_COLOR = "qtc_hs_cal";
       
    80     const char *MONTH_LABEL_COLOR = "qtc_hs_list_item_title_normal";
       
    81     //const char *HIGHLIGHT_LABEL_COLOR = "qtc_hs_list_item_pressed";
       
    82     const char *DATE_BACKGROUND = "qtg_large_calendar_dynamic";
       
    83     //const char *HIGHLIGHT_IMAGE_NAME = "qtg_fr_hsitems_pressed";
       
    84     
       
    85     //From MainLayoutHandler
       
    86     const char WIDGET[] = "CalendarWidget";
       
    87     const char *BACKGROUND_LABEL = "widgetBackground";
       
    88     const char *SEPARATOR_LABEL = "separatorLabel";
       
    89     const char *SEPARATOR_IMAGE_NAME = "qtg_graf_divider_v_thin";
       
    90     const char *BACKGROUND_IMAGE_NAME = "qtg_fr_hswidget_normal";
       
    91     const HbFrameDrawer::FrameType BACKGROUND_FRAME_TYPE = HbFrameDrawer::NinePieces;
       
    92     const HbFrameDrawer::FrameType SEPARATOR_FRAME_TYPE = HbFrameDrawer::OnePiece;
       
    93 }
       
    94 
       
    95 // ======== MEMBER FUNCTIONS ========
       
    96 
       
    97 /*
       
    98  * ContentLayoutHandler::ContentLayoutHandler()
       
    99  */
       
   100 ContentLayoutHandler::ContentLayoutHandler()
       
   101         : mContainer(0),
       
   102           mReminderLabel(0),
       
   103           mUpperLabelShort(0),
       
   104           mUpperLabelLong(0),
       
   105           mMiddleLabel(0),
       
   106           mLowerLabel(0),
       
   107           mIconContainer(0),
       
   108           mIconLabel(0),
       
   109           mNumberLabel(0),
       
   110           mMonthLabel(0),
       
   111           mHighlightLabel(0),
       
   112           mDateIconFrameDrawer(0),
       
   113           mReminderDrawer(0),
       
   114           mEventTimer(0),
       
   115           mAgendaUtil(0),
       
   116           mCalendarKeyManager(0),
       
   117           mDateHighlightActive(0),
       
   118           mContentHighlightActive(0)
       
   119 {
       
   120     LOGS("ContentLayoutHandler::ContentLayoutHandler");
       
   121 
       
   122     mCalendarKeyManager = new XQSettingsManager();
       
   123     unsigned long int key = (unsigned long int)ECalPubSubEventNotification;
       
   124     XQSettingsKey calendarKey(XQSettingsKey::TargetPublishAndSubscribe,
       
   125         KCalPubSubCategory.iUid, key);
       
   126     mCalendarKeyManager->startMonitoring(calendarKey);
       
   127     connect(mCalendarKeyManager, SIGNAL(valueChanged(XQSettingsKey, QVariant)),
       
   128         this, SLOT(calendarChanged(XQSettingsKey, QVariant)));
       
   129 }
       
   130 
       
   131 /*
       
   132  * ContentLayoutHandler::~ContentLayoutHandler()
       
   133  */
       
   134 ContentLayoutHandler::~ContentLayoutHandler()
       
   135 {
       
   136     LOGS("ContentLayoutHandler::~ContentLayoutHandler");
       
   137     unsigned long int key = (unsigned long int)ECalPubSubEventNotification;
       
   138     XQSettingsKey calendarKey(XQSettingsKey::TargetPublishAndSubscribe,
       
   139         KCalPubSubCategory.iUid, key);
       
   140     mCalendarKeyManager->stopMonitoring(calendarKey);
       
   141     delete mCalendarKeyManager;
       
   142 }
       
   143 
       
   144 /*
       
   145  * ContentLayoutHandler::initializeLayout()
       
   146  */
       
   147 void ContentLayoutHandler::initializeLayout(const HbDocumentLoader &layoutLoader,
       
   148     QObject *owner)
       
   149 {
       
   150     this->setParent(owner);
       
   151     this->setObjectName("contentlayouthandler");
       
   152     
       
   153     //From MainLayoutHandler
       
   154     mWidget = (HbWidget*)layoutLoader.findWidget(WIDGET);
       
   155     mBgLabel = qobject_cast<HbLabel*>(layoutLoader.findWidget(BACKGROUND_LABEL));
       
   156     mSeparatorLabel = qobject_cast<HbLabel*>(layoutLoader.findWidget(QString(SEPARATOR_LABEL)));
       
   157 
       
   158     // Discontinue initialization of the main widget is missing
       
   159     if (!mWidget) {
       
   160         return;
       
   161     }
       
   162 
       
   163     addItem(mWidget);
       
   164     mWidget->setVisible(true);
       
   165 
       
   166     // Create main background drawer
       
   167     if (mBgLabel) {
       
   168         HbFrameDrawer* backgroundFrameDrawer = new HbFrameDrawer(BACKGROUND_IMAGE_NAME,
       
   169             BACKGROUND_FRAME_TYPE);
       
   170         mBackgroundLayoutItem = new HbFrameItem(backgroundFrameDrawer);
       
   171         mBgLabel->setBackgroundItem(mBackgroundLayoutItem);
       
   172     }
       
   173     
       
   174     // Create separator graphics drawer
       
   175     HbFrameDrawer* separatorFrameDrawer = new HbFrameDrawer(SEPARATOR_IMAGE_NAME,
       
   176         SEPARATOR_FRAME_TYPE);
       
   177     mSeparatorLayoutItem = new HbFrameItem(separatorFrameDrawer);
       
   178     mSeparatorLabel->setBackgroundItem(mSeparatorLayoutItem);
       
   179 
       
   180     setPreferredSize(mWidget->preferredSize());
       
   181     //END
       
   182     
       
   183     //From DateIconLayoutHandler
       
   184     mIconContainer = qobject_cast<HbWidget*>(layoutLoader.findWidget(QString(ICON_CONTAINER)));
       
   185     mIconLabel = qobject_cast<HbLabel*>(layoutLoader.findWidget(QString(DATE_ICON_LABEL)));
       
   186     mNumberLabel = qobject_cast<HbLabel*>(layoutLoader.findWidget(QString(DATE_ICON_DAYNUMBER)));
       
   187     mMonthLabel = qobject_cast<HbLabel*>(layoutLoader.findWidget(QString(DATE_ICON_MONTHLABEL)));
       
   188     mHighlightLabel = qobject_cast<HbLabel*>(layoutLoader.findWidget(QString(DATE_ICON_HIGHLIGHTLABEL)));
       
   189 
       
   190     // Create background drawer only if day container loading succeeded
       
   191     if (mIconLabel) {
       
   192         mDateIconFrameDrawer = new HbFrameDrawer(DATE_BACKGROUND, DATE_ICON_BACKGROUND_FRAME_TYPE);
       
   193         HbFrameItem* dateIconFrameItem = new HbFrameItem(mDateIconFrameDrawer);
       
   194         mIconLabel->setBackgroundItem(dateIconFrameItem);
       
   195     }
       
   196 
       
   197     setCalendarIconContent();
       
   198     setLabelsColor();
       
   199 
       
   200     // The icon label should never show text
       
   201     if (mIconLabel) {
       
   202         mIconLabel->clear();
       
   203     }
       
   204     if (mHighlightLabel) {
       
   205         mHighlightLabel->clear();
       
   206     }
       
   207 
       
   208     
       
   209     mDateObserver = new DateTimeObserver();
       
   210     connect(mDateObserver, SIGNAL(completed()), this, SIGNAL(dateChanged()));
       
   211     connect(this, SIGNAL(dateChanged()), this, SLOT(updateLayout()));
       
   212     //END
       
   213     
       
   214     // Get widget handles
       
   215     mContainer = qobject_cast<HbWidget*>(
       
   216         layoutLoader.findWidget(QString(CONTENT_CONTAINER)));
       
   217     mUpperLabelShort = qobject_cast<HbLabel*>(
       
   218         layoutLoader.findWidget(QString(UPPER_LABEL_SHORT)));
       
   219     mUpperLabelLong = qobject_cast<HbLabel*>(
       
   220         layoutLoader.findWidget(QString(UPPER_LABEL_LONG)));
       
   221     mMiddleLabel = qobject_cast<HbLabel*>(
       
   222         layoutLoader.findWidget(QString(MIDDLE_LABEL)));
       
   223     mLowerLabel = qobject_cast<HbLabel*>(
       
   224         layoutLoader.findWidget(QString(LOWER_LABEL)));
       
   225     mReminderLabel = qobject_cast<HbLabel*>(
       
   226         layoutLoader.findWidget(QString(REMINDER_ICON_LABEL)));
       
   227 
       
   228     // Create reminder icon drawer only if reminder label loading succeeded.
       
   229     if (mReminderLabel) {
       
   230         // Make sure there's no text in reminder icon label
       
   231         mReminderLabel->clear();
       
   232         // Create drawer for reminder icon
       
   233         mReminderDrawer = new HbFrameDrawer(REMINDER_IMAGE_NAME,
       
   234             REMINDER_FRAME_TYPE);
       
   235         //ownership transferred
       
   236         HbFrameItem* reminderFrameItem = new HbFrameItem(mReminderDrawer);
       
   237         //ownership transferred
       
   238         mReminderLabel->setBackgroundItem(reminderFrameItem);
       
   239     }
       
   240 
       
   241     setLabelsColor();
       
   242     hideLabels();
       
   243     createAgendaUtilsInstance();
       
   244     initializeTimer();
       
   245 }
       
   246 
       
   247 /*
       
   248  * ContentLayoutHandler::calendarChanged()
       
   249  */
       
   250 void ContentLayoutHandler::calendarChanged(const XQSettingsKey& key,
       
   251     const QVariant& value)
       
   252 {
       
   253     LOGS("ContentLayoutHandler::calendarChanged");
       
   254     Q_UNUSED(key);
       
   255     Q_UNUSED(value);
       
   256     updateLayout();
       
   257 }
       
   258 
       
   259 /*
       
   260  * ContentLayoutHandler::updateLayout()
       
   261  */
       
   262 void ContentLayoutHandler::updateLayout()
       
   263 {
       
   264     LOGS("ContentLayoutHandler::updateLayout");
       
   265     decorateContent();
       
   266     setTimerForNextUpdate();
       
   267     //From DateIconLayoutHandler
       
   268     setCalendarIconContent();
       
   269 }
       
   270 
       
   271 /*
       
   272  * ContentLayoutHandler::onThemeChange()
       
   273  */
       
   274 void ContentLayoutHandler::onThemeChange()
       
   275 {
       
   276     LOGS("ContentLayoutHandler::onThemeChange");
       
   277     if (mReminderDrawer) {
       
   278         mReminderDrawer->themeChanged();
       
   279     }
       
   280     
       
   281     if (mDateIconFrameDrawer) {
       
   282         mDateIconFrameDrawer->themeChanged();
       
   283     }
       
   284     
       
   285     if (mBackgroundLayoutItem) {
       
   286         mBackgroundLayoutItem->frameDrawer().themeChanged();
       
   287     }
       
   288     if (mSeparatorLayoutItem) {
       
   289         mSeparatorLayoutItem->frameDrawer().themeChanged();
       
   290     }
       
   291     
       
   292     setLabelsColor();
       
   293 }
       
   294 
       
   295 /*
       
   296  * ContentLayoutHandler::createAgendaUtilsInstance()
       
   297  */
       
   298 void ContentLayoutHandler::createAgendaUtilsInstance()
       
   299 {
       
   300     LOGS("ContentLayoutHandler::createAgendaUtilsInstance");
       
   301     mAgendaUtil = new AgendaUtil(this);
       
   302     connect(mAgendaUtil, SIGNAL(entryViewCreationCompleted(int)),
       
   303         this, SLOT(updateLayout()));
       
   304 }
       
   305 
       
   306 /*
       
   307  * ContentLayoutHandler::initializeTimer()
       
   308  */
       
   309 void ContentLayoutHandler::initializeTimer()
       
   310 {
       
   311     LOGS("ContentLayoutHandler::initializeTimer");
       
   312     mEventTimer = new QTimer();
       
   313     mEventTimer->setSingleShot(true);
       
   314     connect(mEventTimer, SIGNAL(timeout()), this, SLOT(eventTimerExpired()));
       
   315     setTimerForNextUpdate();
       
   316 }
       
   317 
       
   318 /*
       
   319  * ContentLayoutHandler::hideLabels()
       
   320  */
       
   321 void ContentLayoutHandler::hideLabels()
       
   322 {
       
   323     LOGS("ContentLayoutHandler::hideLabels");
       
   324     setLabelVisible(mMiddleLabel, false);
       
   325     setLabelVisible(mUpperLabelShort, false);
       
   326     setLabelVisible(mUpperLabelLong, false);
       
   327     setLabelVisible(mLowerLabel, false);
       
   328     setLabelVisible(mReminderLabel, false);
       
   329 }
       
   330 
       
   331 /*
       
   332  * ContentLayoutHandler::setLabelsColor()
       
   333  */
       
   334 void ContentLayoutHandler::setLabelsColor()
       
   335 {
       
   336     LOGS("ContentLayoutHandler::setLabelsColor");
       
   337     
       
   338     // --- Content layout ---
       
   339     
       
   340     // Choose text colors according to highlight state
       
   341     QString upperLabelColorAttribute;
       
   342     QString lowerLabelColorAttribute;
       
   343     if (mContentHighlightActive) {
       
   344         upperLabelColorAttribute = HIGHLIGHT_LABEL_COLOR;
       
   345         lowerLabelColorAttribute = HIGHLIGHT_LABEL_COLOR;
       
   346     }
       
   347     else {
       
   348         upperLabelColorAttribute = UPPER_LABEL_COLOR;
       
   349         lowerLabelColorAttribute = LOWER_LABEL_COLOR;
       
   350     }
       
   351     QColor upperLabelTextColor(HbColorScheme::color(upperLabelColorAttribute));
       
   352     QColor lowerLabelTextColor(HbColorScheme::color(lowerLabelColorAttribute));
       
   353     
       
   354     // Set upper and middle label text color
       
   355     if (upperLabelTextColor.isValid()) {
       
   356         if (mUpperLabelShort) {
       
   357             mUpperLabelShort->setTextColor(upperLabelTextColor);
       
   358         }
       
   359         if (mUpperLabelLong) {
       
   360             mUpperLabelLong->setTextColor(upperLabelTextColor);
       
   361         }
       
   362         if (mMiddleLabel) {
       
   363             mMiddleLabel->setTextColor(upperLabelTextColor);
       
   364         }
       
   365     }
       
   366 
       
   367     // Set lower label text color
       
   368     if (lowerLabelTextColor.isValid()) {
       
   369         if (mLowerLabel) {
       
   370             mLowerLabel->setTextColor(lowerLabelTextColor);
       
   371         }
       
   372     }
       
   373     
       
   374     // --- DateIconLayoutHandler ---
       
   375     QString numberLabelColorAttribute;
       
   376     QString monthLabelColorAttribute;
       
   377     if (mDateHighlightActive) {
       
   378         numberLabelColorAttribute = NUMBER_LABEL_COLOR;
       
   379         monthLabelColorAttribute = HIGHLIGHT_LABEL_COLOR;
       
   380     }
       
   381     else {
       
   382         numberLabelColorAttribute = NUMBER_LABEL_COLOR;
       
   383         monthLabelColorAttribute = MONTH_LABEL_COLOR;
       
   384     }
       
   385     QColor numberLabelTextColor(HbColorScheme::color(numberLabelColorAttribute));
       
   386     QColor monthLabelTextColor(HbColorScheme::color(monthLabelColorAttribute));
       
   387     
       
   388     // Set day number label text color
       
   389     if (numberLabelTextColor.isValid()) {
       
   390         if (mNumberLabel) {
       
   391             mNumberLabel->setTextColor(numberLabelTextColor);
       
   392         }
       
   393     }
       
   394     // Set month label text color
       
   395     if (monthLabelTextColor.isValid()) {
       
   396         if (mMonthLabel) {
       
   397             mMonthLabel->setTextColor(monthLabelTextColor);
       
   398         }
       
   399     }
       
   400 }
       
   401 
       
   402 /*
       
   403  * ContentLayoutHandler::eventTimerExpired
       
   404  */
       
   405 void ContentLayoutHandler::eventTimerExpired()
       
   406 {
       
   407     LOGS("ContentLayoutHandler::eventTimerExpired");
       
   408     decorateContent();
       
   409     setTimerForNextUpdate();
       
   410 }
       
   411 
       
   412 /*
       
   413  * ContentLayoutHandler::timeToNextCheck()
       
   414  */
       
   415 int ContentLayoutHandler::timeToNextCheck()
       
   416 {
       
   417     LOGS("ContentLayoutHandler::timeToNextCheck");
       
   418     QDateTime now = currentDateTime();
       
   419     int secondsToNextCheck = 0;
       
   420     int currentSeconds = 0;
       
   421     currentSeconds = now.toTime_t();
       
   422     int entryStartTimeSeconds = 0;
       
   423     int entryEndTimeSeconds = 0;
       
   424     int indexEntryToCheck = firstEntryIndex();
       
   425 
       
   426     QDateTime startTime = mAgendaEntryList.at(indexEntryToCheck).startTime();
       
   427     QDateTime endTime = mAgendaEntryList.at(indexEntryToCheck).endTime();
       
   428     entryStartTimeSeconds = startTime.toTime_t();
       
   429     entryEndTimeSeconds = endTime.toTime_t();
       
   430 
       
   431     //check if the event is bigger than half hour
       
   432     if ((entryEndTimeSeconds - entryStartTimeSeconds) < (KHalfHour * KSecondsInMinute)) {
       
   433         secondsToNextCheck = entryEndTimeSeconds - currentSeconds;
       
   434     }
       
   435     else {
       
   436         //check that it's not an all time event.
       
   437         //if it's an all day event there's no need to set the timer because there is already a date check and the widget content
       
   438         //will be updated anyway.
       
   439         if (!isAllDayEvent(mAgendaEntryList.at(indexEntryToCheck))) {
       
   440             secondsToNextCheck = entryStartTimeSeconds + KHalfHour * KSecondsInMinute
       
   441                 - currentSeconds;
       
   442         }
       
   443     }
       
   444 
       
   445     int mSecsToNextCheck = 0;
       
   446     if (secondsToNextCheck > INT_MAX / KSecondInMillisec) {
       
   447         mSecsToNextCheck = INT_MAX;
       
   448     }
       
   449     else {
       
   450         //seconds to milliseconds
       
   451         mSecsToNextCheck = secondsToNextCheck * KSecondInMillisec;
       
   452     }
       
   453 
       
   454     return mSecsToNextCheck;
       
   455 }
       
   456 
       
   457 /*
       
   458  * ContentLayoutHandler::firstEntryIndex()
       
   459  */
       
   460 int ContentLayoutHandler::firstEntryIndex()
       
   461 {
       
   462     int indexEntryToCheck = 0;
       
   463     LOGS("ContentLayoutHandler::firstEntryIndex()");
       
   464 
       
   465     //let's find the first non all day event in the list
       
   466     while (isAllDayEvent(mAgendaEntryList.at(indexEntryToCheck))
       
   467         && (indexEntryToCheck < mAgendaEntryList.count() - 1)) {
       
   468         indexEntryToCheck++;
       
   469     }
       
   470 
       
   471     //if we found the first valid non all day event as last in the array then we have to keep that index
       
   472     if ((indexEntryToCheck == mAgendaEntryList.count() - 1)
       
   473         && isAllDayEvent(mAgendaEntryList.at(indexEntryToCheck))) {
       
   474         indexEntryToCheck = 0;
       
   475     }
       
   476 
       
   477     return indexEntryToCheck;
       
   478 }
       
   479 
       
   480 /*
       
   481  * ContentLayoutHandler::decorateContent()
       
   482  */
       
   483 void ContentLayoutHandler::decorateContent()
       
   484 {
       
   485     LOGS("ContentLayoutHandler::decorateContent");
       
   486     updateList();
       
   487 
       
   488     if (mAgendaEntryList.count()) {
       
   489         removeTodosFrom();
       
   490         //it removes the events that are shorter than 30 minutes and are already passed
       
   491         removePassedEvents();
       
   492     }
       
   493 
       
   494     //there might be only ToDos. Let's check again.
       
   495     if (mAgendaEntryList.count()) {
       
   496         int endEventIndex = 0;
       
   497         if (todayHasValidEvents()) {
       
   498             int numOverlappingEvents = checkOverlappingEvents(0, endEventIndex);
       
   499             if (!numOverlappingEvents) {
       
   500                 showNextEvent();
       
   501             }
       
   502             else {
       
   503                 showOverlappingEvents(KToday, numOverlappingEvents, endEventIndex);
       
   504             }
       
   505         }
       
   506         else {
       
   507             int numOverlappingEvents = checkOverlappingEvents(0, endEventIndex);
       
   508             if (!numOverlappingEvents) {
       
   509                 showNextDaysEvent();
       
   510             }
       
   511             else {
       
   512                 showOverlappingEvents(KComingDays, numOverlappingEvents, endEventIndex);
       
   513             }
       
   514         }
       
   515     }
       
   516     else {
       
   517         showNoEventsNextWeek();
       
   518     }
       
   519     
       
   520     QDateTime date;
       
   521     if (mAgendaEntryList.count()) {
       
   522         date = mAgendaEntryList.at(0).startTime();
       
   523     }
       
   524     else {
       
   525         date = QDateTime::currentDateTime();
       
   526     }
       
   527     
       
   528     emit currentDate(date);
       
   529 }
       
   530 
       
   531 /*
       
   532  * ContentLayoutHandler::updateList()
       
   533  */
       
   534 void ContentLayoutHandler::updateList()
       
   535 {
       
   536     LOGS("ContentLayoutHandler::updateList");
       
   537     
       
   538     // Calculate start of event fetching range
       
   539     QDateTime previousMidnight(currentDateTime().date(), QTime(0, 0));
       
   540     QDateTime passedEventStart(currentDateTime().addSecs(-KPassedEventTimeoutSecs));
       
   541     QDateTime rangeStart;
       
   542     if (previousMidnight < passedEventStart) {
       
   543         rangeStart = previousMidnight;
       
   544     }
       
   545     else {
       
   546         rangeStart = passedEventStart;
       
   547     }
       
   548 
       
   549     // Calculate end of event fetching range
       
   550     QDateTime rangeEnd(previousMidnight.addDays(KDaysInWeek+1));
       
   551     
       
   552     // Fetch events
       
   553     mAgendaEntryList = mAgendaUtil->fetchEntriesInRange(rangeStart, rangeEnd);
       
   554     
       
   555     QString agenda_count = "count: "
       
   556         + QString::number(mAgendaEntryList.count(), 10);
       
   557     LOGS(agenda_count);
       
   558     sortList();
       
   559 }
       
   560 
       
   561 /*
       
   562  * ContentLayoutHandler::removePassedEvents()
       
   563  */
       
   564 void ContentLayoutHandler::removePassedEvents()
       
   565 {
       
   566     LOGS("ContentLayoutHandler::removePassedEvents");
       
   567     for (int i = 0; i < mAgendaEntryList.count(); i++) {
       
   568         //add half hour
       
   569         QDateTime entryStartTime =
       
   570             mAgendaEntryList.at(i).startTime().addSecs(KHalfHour * KSecondsInMinute);
       
   571         LOGS(entryStartTime.toString());
       
   572         LOGS(currentDateTime());
       
   573         //if the entry start time plus half hour is less than current time or it is not but the entry duration is less than half hour (endtime < currenttime)
       
   574         //and the entry is not an all day event then remove it because it is passed.
       
   575         if (((entryStartTime < currentDateTime())
       
   576             || (mAgendaEntryList.at(i).endTime() < currentDateTime()))
       
   577             && !isAllDayEvent(mAgendaEntryList.at(i))) {
       
   578             mAgendaEntryList.removeAt(i);
       
   579             i--;
       
   580         }
       
   581     }
       
   582 }
       
   583 
       
   584 /*
       
   585  * ContentLayoutHandler::currentDateFormat()
       
   586  */
       
   587 QString ContentLayoutHandler::currentDateFormat()
       
   588 {
       
   589     LOGS("ContentLayoutHandler::currentDateFormat");
       
   590     QString format;
       
   591     HbExtendedLocale locale;
       
   592     switch (locale.dateStyle()) {
       
   593         case HbExtendedLocale::American:
       
   594         case HbExtendedLocale::Japanese:
       
   595             format = QString("MMM dd");
       
   596             break;
       
   597         case HbExtendedLocale::European:
       
   598             format = QString("dd MMM");
       
   599             break;
       
   600         default:
       
   601             format = QString("dd:MM");
       
   602             break;
       
   603     }        
       
   604     return format;
       
   605 }
       
   606 
       
   607 /*
       
   608  * ContentLayoutHandler::currentTimeFormat()
       
   609  */
       
   610 QString ContentLayoutHandler::currentTimeFormat()
       
   611 {
       
   612     LOGS("ContentLayoutHandler::currentTimeFormat");
       
   613     QString format;
       
   614     HbExtendedLocale locale;
       
   615     QChar separator = locale.timeSeparator(KHourMinuteSeparatorIndex);
       
   616     format = QString("hh") + separator + QString("mm");
       
   617     if (locale.timeStyle() == HbExtendedLocale::Time12) {
       
   618         if (locale.amPmSymbolPosition() == HbExtendedLocale::Before) {
       
   619             if (locale.amPmSpace()) {
       
   620                 format.prepend(' ');
       
   621             }
       
   622             format.prepend("ap");
       
   623         }
       
   624         else {
       
   625             if (locale.amPmSpace()) {
       
   626                 format.append(' ');
       
   627             }
       
   628             format.append("ap");
       
   629         }    
       
   630     }
       
   631     return format;
       
   632 }
       
   633 
       
   634 /*
       
   635  * ContentLayoutHandler::showNextDaysEvent()
       
   636  */
       
   637 void ContentLayoutHandler::showNextDaysEvent()
       
   638 {
       
   639     LOGS("ContentLayoutHandler::showNextDaysEvent");
       
   640     setLabelVisible(mMiddleLabel, false);
       
   641     setLabelVisible(mUpperLabelShort, true);
       
   642     setLabelVisible(mLowerLabel, true);
       
   643     QString day = mAgendaEntryList.at(0).startTime().date().toString(currentDateFormat());
       
   644     QString startTime = mAgendaEntryList.at(0).startTime().time().toString(currentTimeFormat());
       
   645     QString endTime = mAgendaEntryList.at(0).endTime().time().toString(currentTimeFormat());
       
   646     QString description = mAgendaEntryList.at(0).summary();
       
   647     if (!isAllDayEvent(mAgendaEntryList.at(0))) {
       
   648         //if not all day event show the start time
       
   649         if (mAgendaEntryList.at(0).startTime().date() == mAgendaEntryList.at(0).endTime().date()) {
       
   650             setLabelText(mUpperLabelShort, day + " " + startTime + " " + description);
       
   651             setLabelText(mLowerLabel, hbTrId("txt_calendar_widget_v_dblist_val_no_events_tod"));
       
   652         }
       
   653         else {
       
   654             //if the end date is different from the starting date show that information
       
   655             setLabelText(mUpperLabelShort, day + " " + startTime + "-"
       
   656                 + mAgendaEntryList.at(0).endTime().date().toString(currentDateFormat()) + " "
       
   657                 + description);
       
   658             setLabelText(mLowerLabel, hbTrId("txt_calendar_widget_v_dblist_val_no_events_tod"));
       
   659         }
       
   660     }
       
   661     else {
       
   662         // It's an all day event
       
   663         if (isOneDayAllDayEvent(mAgendaEntryList.at(0))) {
       
   664             // It's a one day all day event
       
   665             setLabelText(mUpperLabelShort, day + " " + description);
       
   666             setLabelText(mLowerLabel, hbTrId("txt_calendar_widget_v_dblist_val_no_events_tod"));
       
   667         }
       
   668         else {
       
   669             // It's a multiple day all day event
       
   670             setLabelText(mUpperLabelShort, day + " - "
       
   671                 + mAgendaEntryList.at(0).endTime().date().addDays(-1).toString(currentDateFormat()) + " "
       
   672                 + description);
       
   673             setLabelText(mLowerLabel, hbTrId("txt_calendar_widget_v_dblist_val_no_events_tod"));
       
   674         }
       
   675     }
       
   676 
       
   677     int timeoffset = mAgendaEntryList.at(0).alarm().timeOffset();
       
   678     if (mAgendaEntryList.at(0).alarm().timeOffset()
       
   679         && mAgendaEntryList.at(0).alarm().timeOffset() != -1) {
       
   680         setLabelVisible(mReminderLabel, true);
       
   681     }
       
   682     else {
       
   683         setLabelVisible(mReminderLabel, false);
       
   684     }
       
   685 }
       
   686 
       
   687 /*
       
   688  * ContentLayoutHandler::showOverlappingEvents()
       
   689  */
       
   690 void ContentLayoutHandler::showOverlappingEvents(int when, int numOverlappingEvents,
       
   691     int aEndEventIndex)
       
   692 {
       
   693     LOGS("ContentLayoutHandler::showOverlappingEvents");
       
   694     if (when == KToday) {
       
   695         setLabelVisible(mMiddleLabel, false);
       
   696         setLabelVisible(mUpperLabelShort, true);
       
   697         setLabelVisible(mLowerLabel, true);
       
   698         QString startTime = mAgendaEntryList.at(0).startTime().time().toString(currentTimeFormat());
       
   699         QString endTime = calculateEndTime(aEndEventIndex);
       
   700         QString day = mAgendaEntryList.at(0).startTime().date().toString(currentDateFormat());
       
   701         if (!isAllDayEvent(mAgendaEntryList.at(0))) {
       
   702             if (mAgendaEntryList.at(0).startTime().date()
       
   703                 == mAgendaEntryList.at(aEndEventIndex).endTime().date()) {
       
   704                 //if not all day event show the start time and end time
       
   705                 setLabelText(mUpperLabelShort, startTime + "-" + endTime);
       
   706                 setLowerLabelOverlapping(numOverlappingEvents);
       
   707             }
       
   708             else {
       
   709                 setLabelText(mUpperLabelShort, startTime + "-"
       
   710                     + mAgendaEntryList.at(aEndEventIndex).endTime().date().toString(
       
   711                         currentDateFormat()) + " "
       
   712                     + mAgendaEntryList.at(aEndEventIndex).endTime().time().toString(
       
   713                         currentTimeFormat()));
       
   714                 setLowerLabelOverlapping(numOverlappingEvents);
       
   715             }
       
   716         }
       
   717         else {
       
   718             // It's an all day event
       
   719             if (isOneDayAllDayEvent(mAgendaEntryList.at(0))) {
       
   720                 // It's a one day all day event
       
   721                 setLabelText(mUpperLabelShort, day);
       
   722                 setLowerLabelOverlapping(numOverlappingEvents);
       
   723             }
       
   724             else {
       
   725                 // It's a multiple day all day event
       
   726                 setLabelText(mUpperLabelShort, day + " - "
       
   727                     + mAgendaEntryList.at(0).endTime().date().addDays(-1).toString(currentDateFormat()));
       
   728                 setLowerLabelOverlapping(numOverlappingEvents);
       
   729             }
       
   730         }
       
   731     }
       
   732     else {
       
   733         setLabelVisible(mMiddleLabel, false);
       
   734         setLabelVisible(mUpperLabelShort, true);
       
   735         setLabelVisible(mLowerLabel, true);
       
   736         QString day = mAgendaEntryList.at(0).startTime().date().toString(currentDateFormat());
       
   737         QString startTime = mAgendaEntryList.at(0).startTime().time().toString(currentTimeFormat());
       
   738         QString endTime = calculateEndTime(aEndEventIndex);
       
   739         if (!isAllDayEvent(mAgendaEntryList.at(0))) {
       
   740             if (mAgendaEntryList.at(0).startTime().date()
       
   741                 == mAgendaEntryList.at(aEndEventIndex).endTime().date()) {
       
   742                 //if not all day event show the start time and end time
       
   743                 setLabelText(mUpperLabelShort, day + " " + startTime + "-" + endTime);
       
   744                 setLowerLabelOverlapping(numOverlappingEvents);
       
   745             }
       
   746             else {
       
   747                 setLabelText(mUpperLabelShort, day + " " + startTime + "-"
       
   748                     + mAgendaEntryList.at(0).endTime().date().toString(currentDateFormat()));
       
   749                 setLowerLabelOverlapping(numOverlappingEvents);
       
   750             }
       
   751         }
       
   752         else {
       
   753             // It's an all day event
       
   754             if (isOneDayAllDayEvent(mAgendaEntryList.at(0))) {
       
   755                 // It's a one day all day event
       
   756                 setLabelText(mUpperLabelShort, day);
       
   757                 setLowerLabelOverlapping(numOverlappingEvents);
       
   758             }
       
   759             else {
       
   760                 // It's a multiple day all day event
       
   761                 setLabelText(mUpperLabelShort, day + " - "
       
   762                     + mAgendaEntryList.at(0).endTime().date().addDays(-1).toString(currentDateFormat()));
       
   763                 setLowerLabelOverlapping(numOverlappingEvents);
       
   764             }
       
   765         }
       
   766     }
       
   767 
       
   768     // Never show reminder icon for overlapping events
       
   769     setLabelVisible(mReminderLabel, false);
       
   770 }
       
   771 
       
   772 /*
       
   773  * ContentLayoutHandler::calculateEndTime()
       
   774  */
       
   775 QString ContentLayoutHandler::calculateEndTime(int& aEndEventIndex)
       
   776 {
       
   777     LOGS("ContentLayoutHandler::calculateEndTime");
       
   778     QDateTime current = mAgendaEntryList.at(0).endTime();
       
   779     int endTimeIndex = 0;
       
   780     for (int i = 0; i < aEndEventIndex + 1; i++) {
       
   781         if (mAgendaEntryList.at(i).endTime() > current) {
       
   782             current = mAgendaEntryList.at(i).endTime();
       
   783             endTimeIndex = i;
       
   784         }
       
   785     }
       
   786 
       
   787     aEndEventIndex = endTimeIndex;
       
   788     return current.time().toString(currentTimeFormat());
       
   789 }
       
   790 
       
   791 /*
       
   792  * ContentLayoutHandler::showNextEvent()
       
   793  */
       
   794 void ContentLayoutHandler::showNextEvent()
       
   795 {
       
   796     LOGS("ContentLayoutHandler::showNextEvent");
       
   797     setLabelVisible(mMiddleLabel, false);
       
   798     setLabelVisible(mUpperLabelShort, true);
       
   799     setLabelVisible(mLowerLabel, true);
       
   800     QString startTime = mAgendaEntryList.at(0).startTime().time().toString(currentTimeFormat());
       
   801     QString endTime = mAgendaEntryList.at(0).endTime().time().toString(currentTimeFormat());
       
   802     QString description = mAgendaEntryList.at(0).summary();
       
   803     if (!isAllDayEvent(mAgendaEntryList.at(0))) {
       
   804         //if not all day event show the start time and end time
       
   805         if (mAgendaEntryList.at(0).startTime().date() == mAgendaEntryList.at(0).endTime().date()) {
       
   806             setLabelText(mUpperLabelShort, startTime + "-" + endTime);
       
   807             setLabelText(mLowerLabel, description);
       
   808         }
       
   809         else {
       
   810             //if the end date is different from the starting date show that information
       
   811             setLabelText(mUpperLabelShort, startTime + "-"
       
   812                 + mAgendaEntryList.at(0).endTime().date().toString(currentDateFormat()) + " "
       
   813                 + endTime);
       
   814             setLabelText(mLowerLabel, description);
       
   815         }
       
   816     }
       
   817     else {
       
   818         // It's an all day event
       
   819         if (isOneDayAllDayEvent(mAgendaEntryList.at(0))) {
       
   820             // It's a one day all day event
       
   821             setLabelText(mUpperLabelShort,
       
   822                 mAgendaEntryList.at(0).startTime().date().toString(currentDateFormat()));
       
   823             setLabelText(mLowerLabel, description);
       
   824         }
       
   825         else {
       
   826             // It's a multiple day all day event
       
   827             setLabelText(mUpperLabelShort,
       
   828                 mAgendaEntryList.at(0).startTime().date().toString(currentDateFormat())
       
   829                 + " - " + mAgendaEntryList.at(0).endTime().date().addDays(-1).toString(
       
   830                 currentDateFormat()));
       
   831             setLabelText(mLowerLabel, description);
       
   832         }
       
   833     }
       
   834 
       
   835     //check if reminder is set
       
   836     if (mAgendaEntryList.at(0).alarm().timeOffset()
       
   837         && mAgendaEntryList.at(0).alarm().timeOffset() != -1) {
       
   838         setLabelVisible(mReminderLabel, true);
       
   839     }
       
   840     else {
       
   841         setLabelVisible(mReminderLabel, false);
       
   842     }
       
   843 }
       
   844 
       
   845 /*
       
   846  * ContentLayoutHandler::showNoEventsNextWeek()
       
   847  */
       
   848 void ContentLayoutHandler::showNoEventsNextWeek()
       
   849 {
       
   850     LOGS("ContentLayoutHandler::showNoEventsNextWeek");
       
   851     setLabelText(mMiddleLabel, hbTrId("txt_calendar_widget_v_dblist_val_no_events_ox"));
       
   852     setLabelVisible(mMiddleLabel, true);
       
   853     setLabelVisible(mUpperLabelShort, false);
       
   854     setLabelVisible(mLowerLabel, false);
       
   855     setLabelVisible(mReminderLabel, false);
       
   856 }
       
   857 
       
   858 /*
       
   859  * ContentLayoutHandler::checkOverlappingEvents()
       
   860  */
       
   861 int ContentLayoutHandler::checkOverlappingEvents(ulong id, int& aEndEventIndex)
       
   862 {
       
   863     LOGS("ContentLayoutHandler::checkOverlappingEvents");
       
   864     int overlappingEvents = 0;
       
   865     AgendaEntry entry = mAgendaEntryList.at(id);
       
   866     for (int i = id + 1; i < mAgendaEntryList.count(); i++) {
       
   867         if ((entry.startTime() <= mAgendaEntryList.at(i).startTime())
       
   868             && (entry.endTime() >= mAgendaEntryList.at(i).startTime())
       
   869             && entry.startTime().date() == mAgendaEntryList.at(i).startTime().date()) {
       
   870             aEndEventIndex = i;
       
   871             overlappingEvents++;
       
   872         }
       
   873     }
       
   874     if (overlappingEvents) {
       
   875         //if there are overlapping events we have to count one more
       
   876         //because the minumum is two.
       
   877         overlappingEvents++;
       
   878     }
       
   879 
       
   880     return overlappingEvents;
       
   881 }
       
   882 
       
   883 /*
       
   884  * ContentLayoutHandler::sortList()
       
   885  */
       
   886 void ContentLayoutHandler::sortList()
       
   887 {
       
   888     LOGS("ContentLayoutHandler::sortList");
       
   889     for (int i = 0; i < mAgendaEntryList.count() - 1; i++) {
       
   890         for (int j = i + 1; j < mAgendaEntryList.count(); j++) {
       
   891             if (mAgendaEntryList.at(i).startTime() > mAgendaEntryList.at(j).startTime()) {
       
   892                 //TODO: Maintain an array of integer ?
       
   893                 mAgendaEntryList.swap(i, j);
       
   894             }
       
   895         }
       
   896     }
       
   897 }
       
   898 
       
   899 /*
       
   900  * ContentLayoutHandler::todayHasValidEvents()
       
   901  */
       
   902 bool ContentLayoutHandler::todayHasValidEvents()
       
   903 {
       
   904     LOGS("ContentLayoutHandler::todayHasValidEvents");
       
   905     QDateTime currentTime = currentDateTime();
       
   906     bool hasValidEvents = false;
       
   907     for (int i = 0; i < mAgendaEntryList.count(); i++) {
       
   908         if ((mAgendaEntryList.at(i).endTime() >= currentTime)
       
   909             && (mAgendaEntryList.at(i).startTime().date() == currentTime.date())) {
       
   910             hasValidEvents = true;
       
   911         }
       
   912     }
       
   913 
       
   914     return hasValidEvents;
       
   915 }
       
   916 
       
   917 /*
       
   918  * ContentLayoutHandler::removeTodosFrom()
       
   919  */
       
   920 void ContentLayoutHandler::removeTodosFrom()
       
   921 {
       
   922     LOGS("ContentLayoutHandler::removeTodosFrom");
       
   923     for (int i = 0; i < mAgendaEntryList.count();) {
       
   924         if ((mAgendaEntryList.at(i).type() == AgendaEntry::TypeTodo)
       
   925             || (mAgendaEntryList.at(i).type() == AgendaEntry::TypeNote)
       
   926             || (mAgendaEntryList.at(i).type() == AgendaEntry::TypeUnknown)) {
       
   927             mAgendaEntryList.removeAt(i);
       
   928         }
       
   929         else {
       
   930             //nothing has been removed. Increment the counter.
       
   931             i++;
       
   932         }
       
   933     }
       
   934 }
       
   935 
       
   936 void ContentLayoutHandler::handleOk(const QVariant& var)
       
   937 {
       
   938     Q_UNUSED(var);
       
   939 
       
   940     LOGS("ContentLayoutHandler::handleOk");
       
   941     mTestResult = 0;
       
   942     emit requestComplete();
       
   943 }
       
   944 
       
   945 void ContentLayoutHandler::handleError(int err, const QString& str)
       
   946 {
       
   947     Q_UNUSED(str);
       
   948 
       
   949     LOGS("ContentLayoutHandler::handleError");
       
   950     mTestResult = err;
       
   951     emit requestError();
       
   952 }
       
   953 
       
   954 void ContentLayoutHandler::highlightOn(QPointF &point)
       
   955 {
       
   956     // --- Content layout ---
       
   957     
       
   958     if (mContainer) {
       
   959         if (mContainer->sceneBoundingRect().contains(point)) {
       
   960             LOGS("[[[Highlight signal in Content area]]]");
       
   961             if (!mContentHighlightActive) {
       
   962                 HbFrameDrawer* highlightDrawer = new HbFrameDrawer(HIGHLIGHT_IMAGE_NAME,
       
   963                     HbFrameDrawer::NinePieces);
       
   964                 HbFrameItem* highlightItem = new HbFrameItem(highlightDrawer);
       
   965                 mContainer->setBackgroundItem(highlightItem);
       
   966                 mContentHighlightActive = true; // must be done before setLabelsColor
       
   967                 setLabelsColor();
       
   968                 mContainer->update();
       
   969             }
       
   970         }
       
   971     }
       
   972     
       
   973     // --- Date layout ---
       
   974     
       
   975     // TODO Don't highlight if already highlighted
       
   976    
       
   977     if (mIconContainer) {
       
   978         if (mIconContainer->sceneBoundingRect().contains(point)) {
       
   979             LOGS("[[[Highlight signal in dateicon area]]]");
       
   980             if (!mDateHighlightActive) {
       
   981                 HbFrameDrawer* highlightDrawer = new HbFrameDrawer(HIGHLIGHT_IMAGE_NAME,
       
   982                     HbFrameDrawer::NinePieces);
       
   983                 HbFrameItem* highlightItem = new HbFrameItem(highlightDrawer);
       
   984                 if (mHighlightLabel) {
       
   985                     mHighlightLabel->setBackgroundItem(highlightItem);
       
   986                 }
       
   987                 mDateHighlightActive = true; // must be done before setLabelsColor
       
   988                 setLabelsColor();
       
   989                 if (mHighlightLabel) {
       
   990                     mHighlightLabel->update();
       
   991                 }
       
   992             }
       
   993         }
       
   994     }
       
   995 }
       
   996 
       
   997 void ContentLayoutHandler::highlightOff()
       
   998 {
       
   999     // --- Content layout ---
       
  1000     
       
  1001     if (mContentHighlightActive) {
       
  1002         QGraphicsItem* background1 = mContainer->backgroundItem();
       
  1003         if (background1) {
       
  1004             background1->hide();
       
  1005         }
       
  1006         mContentHighlightActive = false; // must be done before setLabelsColor
       
  1007         setLabelsColor();
       
  1008         mContainer->update();
       
  1009     }
       
  1010     
       
  1011     // --- Date layout ---
       
  1012     
       
  1013     if (mDateIconFrameDrawer) {
       
  1014         QGraphicsItem* background2 = mHighlightLabel->backgroundItem();
       
  1015         if (background2) {
       
  1016             background2->hide();
       
  1017         }
       
  1018         mDateHighlightActive = false; // must be done before setLabelsColor
       
  1019         setLabelsColor();
       
  1020         mHighlightLabel->update();
       
  1021     }
       
  1022 }
       
  1023 
       
  1024 //TEST FUNCTIONS
       
  1025 
       
  1026 /*
       
  1027  * ContentLayoutHandler::testId()
       
  1028  */
       
  1029 int ContentLayoutHandler::testId()
       
  1030 {
       
  1031     return mTestID;
       
  1032 }
       
  1033 
       
  1034 /*
       
  1035  * ContentLayoutHandler::setTestId()
       
  1036  */
       
  1037 void ContentLayoutHandler::setTestId(int testID)
       
  1038 {
       
  1039     mTestID = testID;
       
  1040 }
       
  1041 
       
  1042 /*
       
  1043  * ContentLayoutHandler::testResult()
       
  1044  */
       
  1045 int ContentLayoutHandler::testResult()
       
  1046 {
       
  1047     return mTestResult;
       
  1048 }
       
  1049 
       
  1050 /*
       
  1051  * ContentLayoutHandler::setTestResult
       
  1052  */
       
  1053 void ContentLayoutHandler::setTestResult(int testResult)
       
  1054 {
       
  1055     mTestResult = testResult;
       
  1056 }
       
  1057 
       
  1058 /*
       
  1059  * ContentLayoutHandler::setTimerForNextUpdate()
       
  1060  */
       
  1061 void ContentLayoutHandler::setTimerForNextUpdate()
       
  1062 {
       
  1063     if (mAgendaEntryList.count()) {
       
  1064         int msecsToNextEvent = timeToNextCheck();
       
  1065         if (msecsToNextEvent > 0) {
       
  1066             mEventTimer->start(msecsToNextEvent);
       
  1067         }
       
  1068     }
       
  1069 }
       
  1070 
       
  1071 QDateTime ContentLayoutHandler::currentDateTime()
       
  1072 {
       
  1073 #ifdef TEST
       
  1074     return QDateTime(QDate(2010, 1, 1), QTime(9, 0, 0));
       
  1075 #else
       
  1076     return QDateTime::currentDateTime();
       
  1077 #endif
       
  1078 }
       
  1079 
       
  1080 void ContentLayoutHandler::setLabelText(HbLabel* label, QString text)
       
  1081 {
       
  1082     if (label) {
       
  1083         label->setPlainText(text);
       
  1084 
       
  1085         if (label == mUpperLabelShort) {
       
  1086             if (mUpperLabelLong) {
       
  1087                 mUpperLabelLong->setPlainText(text);
       
  1088             }
       
  1089         }
       
  1090     }
       
  1091 }
       
  1092 
       
  1093 void ContentLayoutHandler::setLabelVisible(HbLabel* label, bool visible)
       
  1094 {
       
  1095     // State transition table
       
  1096     //  R = reminder label, L = long upper label, S = short upper label
       
  1097     //  rows = current state (label visibility), columns = action
       
  1098     //          [hide R]  [show R]  [hide S]  [show S]
       
  1099     // 0 (slr)   0 (slr)   1 (slR)   0 (slr)   2 (sLr)
       
  1100     // 1 (slR)   0 (slr)   1 (slR)   1 (slR)   5 (SlR)
       
  1101     // 2 (sLr)   2 (sLr)   5 (SlR)   0 (slr)   2 (sLr)
       
  1102     // 3 (sLR)   2 (sLr)   5 (SlR)   1 (slR)   5 (SlR)
       
  1103     // 4 (Slr)   2 (sLr)   5 (SlR)   0 (slr)   2 (sLr)
       
  1104     // 5 (SlR)   2 (sLr)   5 (SlR)   1 (slR)   5 (SlR)
       
  1105     // 6 (SLr)   2 (sLr)   5 (SlR)   0 (slr)   2 (sLr)
       
  1106     // 7 (SLR)   2 (sLr)   5 (SlR)   1 (slR)   5 (SlR)
       
  1107     const int stateTransition[] =
       
  1108         {0,1,0,2,
       
  1109          0,1,1,5,
       
  1110          2,5,0,2,
       
  1111          2,5,1,5,
       
  1112          2,5,0,2, 
       
  1113          2,5,1,5,
       
  1114          2,5,0,2,
       
  1115          2,5,1,5};
       
  1116     
       
  1117     // Logic for showing correct upper label
       
  1118     if (label == mUpperLabelShort || label == mReminderLabel) {
       
  1119         // Make sure all three labels exists
       
  1120         if (mUpperLabelShort && mUpperLabelLong && mReminderLabel) {
       
  1121             int currentState = 0;
       
  1122             currentState += (mReminderLabel->isVisible() ? 1 : 0);
       
  1123             currentState += (mUpperLabelLong->isVisible()  ? 2 : 0);
       
  1124             currentState += (mUpperLabelShort->isVisible() ? 4 : 0);
       
  1125 
       
  1126             int action = -1;
       
  1127             if (label == mReminderLabel) {
       
  1128                 if (visible) {
       
  1129                     action = 1;
       
  1130                 }
       
  1131                 else {
       
  1132                     action = 0;
       
  1133                 }
       
  1134             }
       
  1135             else if (label == mUpperLabelShort) {
       
  1136                 if (visible) {
       
  1137                     action = 3;
       
  1138                 }
       
  1139                 else {
       
  1140                     action = 2;
       
  1141                 }
       
  1142             }
       
  1143             
       
  1144             int newState = stateTransition[currentState * 4 + action];
       
  1145             
       
  1146             if (newState != currentState) {
       
  1147                 mReminderLabel->setVisible(newState & 1);
       
  1148                 mUpperLabelLong->setVisible(newState & 2);
       
  1149                 mUpperLabelShort->setVisible(newState & 4);
       
  1150             }
       
  1151         }
       
  1152     }
       
  1153     else { // Other labels
       
  1154         if (label) {
       
  1155             label->setVisible(visible);
       
  1156         }
       
  1157     }
       
  1158 }
       
  1159 
       
  1160 bool ContentLayoutHandler::isAllDayEvent(AgendaEntry entry)
       
  1161 {
       
  1162     if (entry.startTime().time() == QTime(0, 0)
       
  1163         && entry.endTime().time() == QTime(0, 0)
       
  1164         && entry.startTime().date() < entry.endTime().date()) {
       
  1165         return true;
       
  1166     }
       
  1167     else {
       
  1168         return false;
       
  1169     }
       
  1170 }
       
  1171 
       
  1172 bool ContentLayoutHandler::isOneDayAllDayEvent(AgendaEntry entry)
       
  1173 {
       
  1174     if (isAllDayEvent(entry)
       
  1175         && entry.startTime().date().addDays(1) == entry.endTime().date()) {
       
  1176         return true;
       
  1177     }
       
  1178     else {
       
  1179         return false;
       
  1180     }
       
  1181 }
       
  1182 
       
  1183 void ContentLayoutHandler::setLowerLabelOverlapping(int numberOfEvents)
       
  1184 {
       
  1185     QString trString(hbTrId("txt_calendar_widget_v_dblist_val_l1_events"));
       
  1186     // TODO: Remove the replacing of the parameter, once localized
       
  1187     //       string is corrected.
       
  1188     setLabelText(mLowerLabel, trString.replace("%Ln", "%L1").arg(numberOfEvents));
       
  1189 }
       
  1190 
       
  1191 //from DateIconLayoutHandler
       
  1192 /*
       
  1193  * ContentLayoutHandler::setCalendarIconContent()
       
  1194  */
       
  1195 void ContentLayoutHandler::setCalendarIconContent()
       
  1196 {
       
  1197     LOGS("ContentLayoutHandler::setCalendarIconContent");
       
  1198     if (mNumberLabel) {
       
  1199         mNumberLabel->setPlainText(QString::number(QDateTime::currentDateTime().date().day(), 10));
       
  1200     }
       
  1201     if (mMonthLabel) {
       
  1202         mMonthLabel->setPlainText(QLocale::system().monthName(
       
  1203             QDateTime::currentDateTime().date().month()));
       
  1204     }
       
  1205 }
       
  1206 
       
  1207 //End of file