homesync/contentmanager/mediaservant/src/msstoreserverssetting.cpp
branchIOP_Improvements
changeset 40 08b5eae9f9ff
parent 39 6369bfd1b60d
child 41 b4d83ea1d6e2
equal deleted inserted replaced
39:6369bfd1b60d 40:08b5eae9f9ff
     1 /*
       
     2 * Copyright (c) 2008 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:  CMSStoreServersSetting class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <mediaservant.rsg>
       
    20 #include <StringLoader.h>
       
    21 
       
    22 #include "cmmediaserverfull.h"
       
    23 #include "cmstorerule.h"
       
    24 #include "cmfillrule.h"
       
    25 #include "msstoreserverssetting.h"
       
    26 #include "msmultiselectionsettingpage.h"
       
    27 #include "msconstants.h"
       
    28 #include "msappui.h"
       
    29 #include "msengine.h"
       
    30 #include "msdebug.h"
       
    31 
       
    32 #include "upnpstring.h"
       
    33 
       
    34 // CONSTANTS
       
    35 const TInt KItemArrayGranularity = 3;
       
    36 
       
    37 // --------------------------------------------------------------------------
       
    38 // CMSStoreServersSetting::NewL
       
    39 // --------------------------------------------------------------------------
       
    40 //
       
    41 CMSStoreServersSetting* CMSStoreServersSetting::NewL(
       
    42                                 TInt aIdentifier,
       
    43                                 CCmStoreRule* aRule,
       
    44                                 CMSAppUi& aAppUi,
       
    45                                 TDes& aText )
       
    46     {
       
    47     LOG(_L("[MediaServant]\t CMSStoreServersSetting::NewL"));
       
    48 
       
    49     CMSStoreServersSetting* self = CMSStoreServersSetting::NewLC(
       
    50                                              aIdentifier,
       
    51                                              aRule,
       
    52                                              aAppUi,
       
    53                                              aText );
       
    54 
       
    55     CleanupStack::Pop(self);
       
    56     
       
    57     return self;
       
    58     }
       
    59 
       
    60 // --------------------------------------------------------------------------
       
    61 // CMSStoreServersSetting::NewLC
       
    62 // --------------------------------------------------------------------------
       
    63 //
       
    64 CMSStoreServersSetting* CMSStoreServersSetting::NewLC(
       
    65                                 TInt aIdentifier,
       
    66                                 CCmStoreRule* aRule,
       
    67                                 CMSAppUi& aAppUi,
       
    68                                 TDes& aText )
       
    69     {
       
    70     LOG(_L("[MediaServant]\t CMSStoreServersSetting::NewLC"));
       
    71 
       
    72     CMSStoreServersSetting* self = new (ELeave) CMSStoreServersSetting(
       
    73                                              aIdentifier,
       
    74                                              aRule,
       
    75                                              aAppUi,
       
    76                                              aText );
       
    77 
       
    78     CleanupStack::PushL(self);
       
    79     self->ConstructL();
       
    80 
       
    81     return self;
       
    82     }
       
    83     
       
    84 // --------------------------------------------------------------------------
       
    85 // CMSStoreServersSetting::ConstructL
       
    86 // --------------------------------------------------------------------------
       
    87 //
       
    88 void CMSStoreServersSetting::ConstructL()
       
    89     {
       
    90     LOG(_L("[MediaServant]\t CMSStoreServersSetting::ConstructL"));
       
    91 
       
    92     iSettingText = HBufC::NewL( KMaxFileName );
       
    93 
       
    94     // Get media server list
       
    95     iServers = iAppUi.MSEngine()->GetMediaServersL();
       
    96 
       
    97     // create item array
       
    98     CreateSelectionItemListL();
       
    99 
       
   100     SetSettingItemTextL();
       
   101     }
       
   102 
       
   103 // --------------------------------------------------------------------------
       
   104 // CMSStoreServersSetting::CMSRuleServersSetting
       
   105 // --------------------------------------------------------------------------
       
   106 //
       
   107 CMSStoreServersSetting::CMSStoreServersSetting(
       
   108     TInt aIdentifier,
       
   109     CCmStoreRule* aRule,
       
   110     CMSAppUi& aAppUi,
       
   111     TDes& aText
       
   112      ) :
       
   113     CAknTextSettingItem( aIdentifier, aText ),
       
   114     iRule( aRule ),
       
   115     iAppUi( aAppUi )
       
   116     {
       
   117     }
       
   118 // --------------------------------------------------------------------------
       
   119 // CMSStoreServersSetting::~CMSStoreServersSetting()
       
   120 // --------------------------------------------------------------------------
       
   121 //
       
   122 CMSStoreServersSetting::~CMSStoreServersSetting()
       
   123     {
       
   124     delete iSettingText;
       
   125 
       
   126     if ( iItemArray )
       
   127         {
       
   128         iItemArray->ResetAndDestroy();
       
   129         delete iItemArray;
       
   130         }
       
   131 
       
   132     if ( iServers )
       
   133         {
       
   134         iServers->ResetAndDestroy();
       
   135         iServers->Close();
       
   136         delete iServers;
       
   137         }
       
   138     }
       
   139 
       
   140 // --------------------------------------------------------------------------
       
   141 // CMSStoreServersSetting::EditItemL( TBool aCalledFromMenu )
       
   142 // --------------------------------------------------------------------------
       
   143 //
       
   144 void CMSStoreServersSetting::EditItemL( TBool /*aCalledFromMenu*/ )
       
   145     {
       
   146     LOG(_L("[MediaServant]\t CMSStoreServersSetting::EditItemL"));
       
   147 
       
   148     // Create setting page
       
   149     CAknSettingPage* dlg = CMSMultiselectionSettingPage::NewL(
       
   150             R_MS_STORE_SETTINGS_LOCATION_SETTING_PAGE,
       
   151             *iItemArray, EFalse );
       
   152     // launch setting page
       
   153     if ( dlg->ExecuteLD( CAknSettingPage::EUpdateWhenChanged ) )
       
   154         {
       
   155         DeleteAllServersFromRuleL();
       
   156 
       
   157         for ( TInt index = 0; index < iItemArray->Count(); index++ )
       
   158                 {
       
   159                 CSelectableItem* item = iItemArray->At( index );
       
   160                 TBool selected = item->SelectionStatus();
       
   161 
       
   162                 CCmMediaServerFull* server = (*iServers)[index];
       
   163                 // get server UDN
       
   164                 TPtrC8 serverUDN = server->MediaServer();
       
   165 
       
   166                 // add server
       
   167                 if ( item->SelectionStatus() )
       
   168                     {
       
   169                     // set server for rule
       
   170                     iRule->AddMediaServerL( serverUDN );
       
   171                     }
       
   172                 }
       
   173 
       
   174         SetSettingItemTextL();
       
   175         LoadL();
       
   176         // show value on screen
       
   177         UpdateListBoxTextL();
       
   178         }
       
   179     }
       
   180 
       
   181 // --------------------------------------------------------------------------
       
   182 // CMSStoreServersSetting::CreateSelectionItemListL
       
   183 // Creates selection item list
       
   184 // --------------------------------------------------------------------------
       
   185 //
       
   186 void CMSStoreServersSetting::CreateSelectionItemListL()
       
   187     {
       
   188     if ( iItemArray )
       
   189         {
       
   190         iItemArray->ResetAndDestroy();
       
   191         delete iItemArray;
       
   192         iItemArray = NULL;
       
   193         }
       
   194 
       
   195     iItemArray =
       
   196             new ( ELeave ) CSelectionItemList( KItemArrayGranularity );
       
   197 
       
   198     TBuf8<KMaxFileName> itemText;
       
   199 
       
   200     TInt serverCount(0);
       
   201     if ( iServers )
       
   202         {
       
   203         serverCount = iServers->Count();
       
   204         }
       
   205 
       
   206     // Cycle trough all servers
       
   207     for ( TInt idx = 0; idx < serverCount; idx++ )
       
   208         {
       
   209         // get server
       
   210         CCmMediaServerFull* server = (*iServers)[idx];
       
   211         // show only copy capable devices
       
   212         if ( server->StoreUsage() )
       
   213             {
       
   214             HBufC* devName =
       
   215                     UpnpString::ToUnicodeL( server->MediaServerName() );
       
   216             CleanupStack::PushL( devName );
       
   217             CSelectableItem* item = new ( ELeave ) CSelectableItem(
       
   218                                                     *devName, EFalse);
       
   219 
       
   220             CleanupStack::PushL( item );
       
   221             // item must be constructed
       
   222             item->ConstructL();
       
   223 
       
   224             // count of servers set to rule
       
   225             TInt ruleServerCount = iRule->MediaServerCount();
       
   226 
       
   227             // Cycle trough all servers
       
   228             for (TInt index = 0; index < ruleServerCount; index++ )
       
   229                 {
       
   230                 const TDesC8& ruleServerUDN = iRule->MediaServerL( index );
       
   231 
       
   232                 if (ruleServerUDN == server->MediaServer()) // compare UDNs
       
   233                     {
       
   234                     // found - break this loop
       
   235                     index = ruleServerCount;
       
   236                     // found - mark selected
       
   237                     item->SetSelectionStatus( ETrue );
       
   238                     iSelectedItemCount++;
       
   239                     }
       
   240                 }
       
   241             // append item to list
       
   242             iItemArray->AppendL(item);
       
   243             CleanupStack::Pop( item );
       
   244             CleanupStack::PopAndDestroy( devName );
       
   245             }
       
   246         else    // we don't need this kind of servers anymore so remove it
       
   247             {
       
   248             delete server;
       
   249             iServers->Remove( idx );
       
   250             idx--; // remove transfers next item to current index
       
   251             serverCount--; // servers on the list were reduced
       
   252             }
       
   253         }
       
   254     }
       
   255 
       
   256 // --------------------------------------------------------------------------
       
   257 // CMSStoreServersSetting::DeleteAllServersFromRuleL
       
   258 // Deletes all servers from rule
       
   259 // --------------------------------------------------------------------------
       
   260 //
       
   261 void CMSStoreServersSetting::DeleteAllServersFromRuleL()
       
   262     {
       
   263     LOG(_L("[MediaServant]\t CMSRuleServersSetting::\
       
   264     CMSStoreServersSetting"));
       
   265 
       
   266     // count of servers set to rule
       
   267     TInt ruleServerCount = iRule->MediaServerCount();
       
   268 
       
   269     for ( TInt index = ruleServerCount-1; index >= 0; index-- )
       
   270         {
       
   271         const TDesC8& ruleServerUDN = iRule->MediaServerL( index );
       
   272         iRule->DeleteMediaServer( ruleServerUDN );
       
   273         }
       
   274     }
       
   275 
       
   276 // --------------------------------------------------------------------------
       
   277 // CMSStoreServersSetting::CountSelectedItems
       
   278 // Counts selected items
       
   279 // --------------------------------------------------------------------------
       
   280 //
       
   281 TInt CMSStoreServersSetting::CountSelectedItems()
       
   282     {
       
   283     LOG(_L("[MediaServant]\t CMSRuleServersSetting::\
       
   284     CMSStoreServersSetting"));
       
   285 
       
   286     TInt count(0);
       
   287 
       
   288     for ( TInt index = 0; index < iItemArray->Count(); index++ )
       
   289         {
       
   290         if ( (*iItemArray)[index]->SelectionStatus() )
       
   291             {
       
   292             count++;
       
   293             }
       
   294         }
       
   295 
       
   296     return count;
       
   297     }
       
   298 
       
   299 // --------------------------------------------------------------------------
       
   300 // CMSStoreServersSetting::SetSettingItemTextL
       
   301 // Sets setting item secondary text according to selected items
       
   302 // --------------------------------------------------------------------------
       
   303 //
       
   304 void CMSStoreServersSetting::SetSettingItemTextL()
       
   305     {
       
   306         TInt count = CountSelectedItems();
       
   307 
       
   308         HBufC* itemText = NULL;
       
   309         // no items selected
       
   310         if ( count == 0 )
       
   311             {
       
   312             itemText = StringLoader::LoadLC( R_MS_NO_SELECTED_ITEMS );
       
   313             iSettingText->Des().Copy( *itemText );
       
   314             }
       
   315         // just one item selected
       
   316         else if ( count == 1 )
       
   317             {
       
   318             TInt itemCount = iItemArray->Count();
       
   319             for ( TInt index = 0; index < itemCount; index++ )
       
   320                 {
       
   321                 CSelectableItem* item = (*iItemArray)[ index ];
       
   322                 if ( item->SelectionStatus() )
       
   323                     {
       
   324                     iSettingText->Des().Copy( item->ItemText() );
       
   325                     index = itemCount; // break loop
       
   326                     }
       
   327                 }
       
   328             }
       
   329         // more than one item selected
       
   330         else
       
   331             {
       
   332             itemText = StringLoader::LoadLC( R_MS_ITEM_DEVICES, count );
       
   333 
       
   334             // do number conversion
       
   335             TPtr ptr = itemText->Des();
       
   336             AknTextUtils::DisplayTextLanguageSpecificNumberConversion( ptr );
       
   337 
       
   338             iSettingText->Des().Copy( *itemText );
       
   339             }
       
   340 
       
   341         if ( itemText )
       
   342             {
       
   343             CleanupStack::PopAndDestroy( itemText );
       
   344             }
       
   345 
       
   346          // Set new value
       
   347          SetExternalText( *iSettingText );
       
   348     }
       
   349 // End of File
       
   350