qtinternetradio/ui/src/iropenwebaddressview.cpp
changeset 0 09774dfdd46b
child 3 ee64f059b8e1
equal deleted inserted replaced
-1:000000000000 0:09774dfdd46b
       
     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:
       
    15 *
       
    16 */
       
    17 #include <hbapplication.h>
       
    18 #include <hbmenu.h>
       
    19 #include <hbtoolbar.h>
       
    20 #include <hbpushbutton.h>
       
    21 #include <hblineedit.h>
       
    22 #include <hbinstance.h>
       
    23 
       
    24 #include "irabstractviewmanager.h"
       
    25 #include "irapplication.h"
       
    26 #include "irplaycontroller.h"
       
    27 #include "iropenwebaddressview.h"
       
    28 #include "irqisdsdatastructure.h"
       
    29 #include "irqfavoritesdb.h"
       
    30 #include "irqnetworkcontroller.h"
       
    31 #include "irqutility.h"
       
    32 #include "irlineeditor.h"
       
    33 #include "irqenums.h"
       
    34 #include "iruidefines.h"
       
    35 
       
    36 // Const strings
       
    37 const char* OPEN_WEB_ADDRESS_VIEW_OBJECT_NAME = "ex-IRQOpenWebAddressView"; // object name in the XML
       
    38 
       
    39 IROpenWebAddressView::IROpenWebAddressView(IRApplication* aApplication, TIRViewId aViewId) :
       
    40     IRBaseView(aApplication, aViewId),
       
    41     iUrl(NULL),
       
    42     iName(NULL),
       
    43     iDescription(NULL),
       
    44     iNameClicked(false),
       
    45     iPlayButton(NULL),
       
    46     iAdd2FavButton(NULL)
       
    47 {
       
    48     connect( getViewManager(), SIGNAL( orientationChanged(Qt::Orientation) ),
       
    49              this, SLOT( handleOrientationChanged(Qt::Orientation) ) );
       
    50     
       
    51     // Create widget hierarchy
       
    52     setObjectName(OPEN_WEB_ADDRESS_VIEW_OBJECT_NAME);
       
    53 
       
    54     // List existing root elements - this allows us to refer to objects in the XML 
       
    55     // which are created outside the document.
       
    56     QObjectList roots;
       
    57     roots.append(this); // IROpenWebAddressView is referred in the XML file
       
    58     iLoader.setObjectTree(roots);
       
    59 
       
    60     // Load the XML file
       
    61     iLoader.load(OPEN_WEB_ADDRESS_VIEW_LAYOUT_FILENAME);
       
    62 
       
    63     // Find the HbLineEdit objects
       
    64     iUrl = qobject_cast<HbLineEdit *> (iLoader.findObject("streamURL"));
       
    65     iName = qobject_cast<IrLineEditor *> (iLoader.findObject("stationName"));
       
    66     iDescription = qobject_cast<HbLineEdit *> (iLoader.findObject("description"));
       
    67 
       
    68     initButtons();
       
    69 
       
    70     // This view need not to be stacked.
       
    71     setFlag(EViewFlag_UnStackable);
       
    72 
       
    73     connect(iName, SIGNAL(pressed()), this, SLOT(nameEditorClicked()));
       
    74     connect(iNetworkController, SIGNAL(networkRequestNotified(IRQNetworkEvent)),
       
    75     this, SLOT(networkRequestNotified(IRQNetworkEvent)));
       
    76     
       
    77     connect(iUrl, SIGNAL(textChanged(const QString&)),
       
    78     this, SLOT(urlEditorTextChanged(const QString&)));
       
    79 
       
    80     handleOrientationChanged(getViewManager()->orientation());
       
    81 }
       
    82 
       
    83 IROpenWebAddressView::~IROpenWebAddressView()
       
    84 {
       
    85 }
       
    86 
       
    87 /*
       
    88  * Description : from base class IRBaseView.
       
    89  *               handle view commands.
       
    90  * Parameters  : aCommand : see the definition of TIRViewCommand
       
    91  * Return      : EIR_DoDefault : caller does default handling
       
    92  *               EIR_NoDefault : caller doesn't do default handling
       
    93  */
       
    94 TIRHandleResult IROpenWebAddressView::handleCommand(TIRViewCommand aCommand,
       
    95         TIRViewCommandReason aReason)
       
    96 {
       
    97     Q_UNUSED(aReason);
       
    98 
       
    99     switch (aCommand)
       
   100     {
       
   101     case EIR_ViewCommand_ACTIVATED:
       
   102         initDetails();
       
   103         return EIR_NoDefault;
       
   104 
       
   105     case EIR_ViewCommand_DEACTIVATE:
       
   106     default:
       
   107         break;
       
   108     }
       
   109 
       
   110     return EIR_DoDefault;
       
   111 }
       
   112 
       
   113 /*
       
   114  * Description : initialize the details.
       
   115  */
       
   116 void IROpenWebAddressView::initDetails()
       
   117 {
       
   118     // Set the initial text for line editor.
       
   119     iUrl->setText("http://");
       
   120     iName->setText(hbTrId("txt_irad_info_unnamed"));
       
   121     iDescription->setText("");
       
   122     iNameClicked = false;
       
   123 }
       
   124 
       
   125 /*
       
   126  * Description : initialize the buttons.
       
   127  */
       
   128 void IROpenWebAddressView::initButtons()
       
   129 {
       
   130     // Find the HbAction objects
       
   131     iPlayButton = qobject_cast<HbPushButton *> (iLoader.findObject("playButton"));
       
   132     iAdd2FavButton = qobject_cast<HbPushButton *> (iLoader.findObject("add2FavButton"));
       
   133     
       
   134     connect(iPlayButton, SIGNAL(released()), this, SLOT(play()));
       
   135     connect(iAdd2FavButton, SIGNAL(released()), this, SLOT(add2Fav()));
       
   136 }
       
   137 
       
   138 /*
       
   139  * Description : add the station to favorites.
       
   140  */
       
   141 void IROpenWebAddressView::add2Fav()
       
   142 {
       
   143     // Create a IRQPreset using the inputted information.
       
   144     IRQPreset preset;
       
   145 
       
   146     if (!initPreset(preset))
       
   147     {
       
   148         return;
       
   149     }
       
   150 
       
   151     // Add to favorites.
       
   152     int retValue = iFavorites->addPreset(preset);
       
   153 
       
   154     // Show the information from favorites.
       
   155     if (EIRQErrorAlreadyExist == retValue)
       
   156     {
       
   157         popupNote(hbTrId("txt_irad_info_favorite_updated"), HbMessageBox::MessageTypeInformation);
       
   158     }
       
   159     else if (EIRQErrorNone == retValue)
       
   160     {
       
   161         popupNote(hbTrId("txt_irad_info_added_to_favorites"), HbMessageBox::MessageTypeInformation);
       
   162     }
       
   163     else if (EIRQErrorOutOfMemory == retValue)
       
   164     {
       
   165         popupNote(hbTrId("txt_irad_info_can_not_add_more"), HbMessageBox::MessageTypeInformation);
       
   166     }
       
   167     else
       
   168     {
       
   169         Q_ASSERT(false);
       
   170     }
       
   171 }
       
   172 
       
   173 /*
       
   174  * Description : Play this station.
       
   175  */
       
   176 void IROpenWebAddressView::play()
       
   177 {
       
   178     // Create a IRQPreset using the inputted information
       
   179     IRQPreset preset;
       
   180 
       
   181     if (!initPreset(preset))
       
   182     {
       
   183         return;
       
   184     }
       
   185 
       
   186     setUseNetworkReason(EIR_UseNetwork_OpenWebAddress);
       
   187 
       
   188     // Verify the connectivity
       
   189     if (false == iApplication->verifyNetworkConnectivity())
       
   190     {
       
   191         return;
       
   192     }
       
   193 
       
   194     setUseNetworkReason(EIR_UseNetwork_NoReason);
       
   195 
       
   196     // Play this preset
       
   197     iPlayController->connectToChannel(&preset,EIRQAdhocManual);
       
   198 }
       
   199 
       
   200 /*
       
   201  * Description : Handle the network event from network controller.
       
   202  */
       
   203 void IROpenWebAddressView::networkRequestNotified(IRQNetworkEvent aEvent)
       
   204 {
       
   205     if (getViewManager()->currentView() != this)
       
   206     {
       
   207         return;
       
   208     }
       
   209     
       
   210     switch (aEvent)
       
   211     {
       
   212     case EIRQNetworkConnectionEstablished:
       
   213         iApplication->closeConnectingDialog();
       
   214 
       
   215         if (EIR_UseNetwork_OpenWebAddress == getUseNetworkReason())
       
   216         {
       
   217             play();
       
   218         }
       
   219 
       
   220         setUseNetworkReason(EIR_UseNetwork_NoReason);
       
   221         break;
       
   222         
       
   223     default:
       
   224         break;
       
   225     }
       
   226 }
       
   227 
       
   228 /*
       
   229  * Description : Clear the name editor.
       
   230  */
       
   231 void IROpenWebAddressView::nameEditorClicked()
       
   232 {    
       
   233     if(!iNameClicked)
       
   234     {
       
   235         iName->setText("");
       
   236         iNameClicked = true;
       
   237     }     
       
   238 }
       
   239 
       
   240 /*
       
   241  * Description : initialize a preset.
       
   242  * return parameter: true, initialization succeeds; vice versa.
       
   243  */
       
   244 bool IROpenWebAddressView::initPreset(IRQPreset &aPreset)
       
   245 {
       
   246     IRQChannelServerURL server;
       
   247 
       
   248     server.url = iUrl->text();
       
   249     server.url.remove(" ");
       
   250 
       
   251     // check the URL
       
   252     if (!IRQUtility::isValidUrl(server.url))
       
   253     {
       
   254         popupNote(hbTrId("txt_irad_info_invalid_link_please_change_it"), HbMessageBox::MessageTypeInformation);
       
   255         return false;
       
   256     }
       
   257 
       
   258     // Give it an initial value, supposed to be 32 kbps.
       
   259     server.bitrate = 32;
       
   260     server.serverName = iName->text();
       
   261     if (0 == server.serverName.size())
       
   262     {
       
   263         server.serverName = hbTrId("txt_irad_info_unnamed");
       
   264     }
       
   265 
       
   266     aPreset.insertChannelServer(server);
       
   267     aPreset.name = server.serverName;
       
   268     aPreset.description = iDescription->text();
       
   269     aPreset.shortDesc = aPreset.description;
       
   270     aPreset.type = 0;     
       
   271     aPreset.presetId = 0;
       
   272     aPreset.uniqID = 0;
       
   273 
       
   274     return true;
       
   275 }
       
   276 
       
   277 
       
   278 void IROpenWebAddressView::urlEditorTextChanged(const QString &aString)
       
   279 {
       
   280     if(aString.size() < 8)
       
   281     {
       
   282         iPlayButton->setEnabled(false);
       
   283         iAdd2FavButton->setEnabled(false);
       
   284     }   
       
   285     else
       
   286     {
       
   287         iPlayButton->setEnabled(true);
       
   288         iAdd2FavButton->setEnabled(true);
       
   289     }
       
   290 }
       
   291 
       
   292 /*
       
   293  * Description : resize the container if the direction changes.
       
   294  */
       
   295 void IROpenWebAddressView::handleOrientationChanged(Qt::Orientation aOrientation)
       
   296 {
       
   297     // Load the XML file
       
   298     if (aOrientation == Qt::Vertical)
       
   299     {
       
   300         iLoader.load(OPEN_WEB_ADDRESS_VIEW_LAYOUT_FILENAME, "portrait");
       
   301     }
       
   302     else
       
   303     {
       
   304         iLoader.load(OPEN_WEB_ADDRESS_VIEW_LAYOUT_FILENAME, "landscape");
       
   305     }
       
   306 }