+#include "UIManager.h"
+
+#include <QDateTime>
+#include <QTime>
+
+#include "Engine.h"
+#include "WindowManager.h"
+#include "ViewBase.h"
+#include "WeeklyViewWidget.h"
+#include "SettingsView.h"
+#include "RoomStatusIndicatorWidget.h"
+#include "PasswordDialog.h"
+#include "MeetingInfoDialog.h"
+#include "ProgressBar.h"
+#include "CommunicationManager.h"
+#include "Configuration.h"
+#include "DisplaySettings.h"
+
+#include <QtDebug>
+
+#define QT_DELETE(X) \
+ if ( X != 0 ) \
+ { \
+ delete X; \
+ X = 0; \
+ }
+
+UIManager::UIManager( Engine *aEngine, WindowManager *aWindowManager ) :
+ iEngine( aEngine ),
+ iWindowManager( aWindowManager ),
+ iWeeklyView( 0 ),
+ iSettingsView( 0 ),
+ iRoomStatusIndicator( 0 ),
+ iPasswordDialog( 0 ),
+ iProgressBar( 0 ),
+ iMeetingInfo( 0 )
+{
+ if ( iEngine == 0 ) return;
+ if ( iWindowManager == 0 ) return;
+
+ createWeeklyView();
+ createSettingsView();
+ createRoomStatusIndicator();
+ createPasswordDialog();
+ createProgressBar();
+ createMeetingInfoDialog();
+}
+
+UIManager::~UIManager()
+{
+ iEngine = 0;
+ iWindowManager = 0;
+
+ QT_DELETE( iMeetingInfo );
+ QT_DELETE( iProgressBar );
+ QT_DELETE( iPasswordDialog );
+ QT_DELETE( iRoomStatusIndicator );
+ QT_DELETE( iSettingsView );
+ QT_DELETE( iWeeklyView );
+}
+
+void UIManager::showMainView()
+{
+ iWindowManager->showView( iWeeklyView );
+}
+
+void UIManager::showProgressBar( QString aText )
+{
+ if ( iProgressBar != 0 )
+ {
+ iProgressBar->update( tr("Changing current operation mode.") );
+ iWindowManager->showDialog( iProgressBar );
+ }
+}
+
+// ===============================================
+// INITIALIZE THE UIMANAGER
+void UIManager::createWeeklyView()
+{
+ iWeeklyView = new WeeklyViewWidget( QDateTime::currentDateTime(), iEngine->iConfiguration );
+
+ // Connect signals
+ connect( iWeeklyView, SIGNAL( settingsButtonClicked() ), this, SLOT( settingsViewRequest() ) );
+ connect( iWeeklyView, SIGNAL( currentRoomChanged( Room * ) ), this, SLOT( currentRoomChanged( Room * ) ) );
+}
+
+void UIManager::createSettingsView()
+{
+ iSettingsView = new SettingsView;
+
+ // Connect signals
+ connect( iSettingsView, SIGNAL( okClicked() ), this, SLOT( settingsOkClicked() ) );
+}
+
+void UIManager::createRoomStatusIndicator()
+{
+ iRoomStatusIndicator = new RoomStatusIndicatorWidget( iEngine->defaultRoom(), Room::FreeStatus, QTime::currentTime(), iEngine->iConfiguration->displaySettings()->dateFormat() );
+}
+
+void UIManager::createPasswordDialog()
+{
+ iPasswordDialog = new PasswordDialog( iEngine->iConfiguration->adminPassword(), tr("UIManager::createPasswordDialog"), tr("UIManager::createPasswordDialog") );
+}
+
+void UIManager::createProgressBar()
+{
+ iProgressBar = new ProgressBar( tr("CHANGE THIS") );
+
+ connect( iProgressBar, SIGNAL( cancelled() ), this, SLOT( progressBarCancelled() ) );
+ connect( iProgressBar, SIGNAL( cancelled() ), iEngine, SLOT( progressBarCancelled() ) );
+}
+
+void UIManager::createMeetingInfoDialog()
+{
+
+}
+
+void UIManager::connectDeviceManager( DeviceManager *aDeviceManager )
+{
+ connect( aDeviceManager, SIGNAL( changeModeOrdered( DeviceManager::OperationMode ) ),
+ this, SLOT( changeModeOrdered( DeviceManager::OperationMode ) ) );
+
+ connect( aDeviceManager, SIGNAL( changingMode( const QString & ) ), iProgressBar, SLOT( update( const QString & ) ) );
+}
+
+void UIManager::connectCommunicationManager( CommunicationManager *aCommunicationManager )
+{
+ // To communication manager
+ connect( iWeeklyView, SIGNAL( meetingActivated( Meeting * ) ), aCommunicationManager, SLOT( fetchMeetingDetails( Meeting * ) ) );
+
+ // From communication manager
+ connect( aCommunicationManager, SIGNAL( meetingsFetched( const QList<Meeting *> ) ), this, SLOT( meetingsFetched( const QList<Meeting *> ) ) );
+ connect( aCommunicationManager, SIGNAL( meetingDetailsFetched( Meeting & ) ), this, SLOT( meetingDetailsFetched( Meeting & ) ) );
+}
+
+// ============================================
+// UIMANAGER SLOTS
+void UIManager::settingsViewRequest()
+{
+ // Show the settings view and stop the idle timer
+ if ( iSettingsView != 0 )
+ {
+ iWindowManager->showView( static_cast<ViewBase *>( iSettingsView ) );
+ iEngine->stopIdleTimeCounter();
+ }
+}
+
+void UIManager::settingsOkClicked()
+{
+ // Show the weekly view and restart the idle timer
+ if ( iWeeklyView != 0 )
+ {
+ iWindowManager->showView( static_cast<ViewBase *>( iWeeklyView ) );
+ iEngine->startIdleTimeCounter();
+ }
+}
+
+void UIManager::meetingsFetched( const QList<Meeting*> &aMeetings )
+{
+
+}
+
+void UIManager::meetingDetailsFetched(Meeting &aDetailedMeeting)
+{
+ if ( iMeetingInfo != 0 )
+ {
+ iMeetingInfo->setMeeting( &aDetailedMeeting );
+ iWindowManager->showDialog( static_cast<QDialog *>( iMeetingInfo ) );
+ }
+}
+
+void UIManager::roomStatusIndicatorRequested()
+{
+ if ( iRoomStatusIndicator != 0 )
+ {
+ iWindowManager->showView( static_cast<ViewBase *>( iRoomStatusIndicator ) );
+ iEngine->stopIdleTimeCounter();
+ }
+}
+
+void UIManager::previousViewRestored()
+{
+ iEngine->startIdleTimeCounter();
+}
+
+void UIManager::progressBarCancelled()
+{
+ // TODO : Close progress bar
+}
+
+void UIManager::changeModeOrdered( DeviceManager::OperationMode aMode )
+{
+ qDebug() << "[UIManager::changeModeOrdered] <Invoked>";
+
+ QString message = tr( "You are about to change operation mode to %1." )
+ .arg( iEngine->iDevice->operationModeToString( aMode ) );
+
+ if ( iPasswordDialog != 0 )
+ {
+ // TODO : Set the new text for password dialog
+ iWindowManager->showDialog( static_cast<QDialog *>( iPasswordDialog ) );
+ }
+}
+
+void UIManager::currentRoomChanged(Room *aRoom)
+{
+ if ( iWeeklyView != 0 )
+ {
+ QDateTime from = QDateTime( iWeeklyView->beginnigOfShownWeek() );
+ QDateTime to = QDateTime( from.addDays( 8 ) );
+ iEngine->fetchMeetings( from, to, aRoom );
+ }
+}
+
+void UIManager::updateTime(QDateTime aDateTime)
+{
+ if ( iWeeklyView != 0 )
+ {
+ iWeeklyView->setCurrentDateTime( aDateTime );
+ }
+}