idlehomescreen/nativeuicontroller/src/aitoolbarrenderer.cpp
changeset 0 f72a12da539e
child 9 f966699dea19
equal deleted inserted replaced
-1:000000000000 0:f72a12da539e
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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:  Touch Toolbar
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "aitoolbarrenderer.h"
       
    20 #include "aifweventhandler.h"
       
    21 #include "aicontentobserver.h"
       
    22 #include "activeidle2domaincrkeys.h"
       
    23 #include "activeidle2domainpskeys.h"
       
    24 #include "aiscutplugindomaincrkeys.h"
       
    25 #include <aipropertyextension.h>
       
    26 #include "ainativeuiplugins.h"
       
    27 
       
    28 #include <AiNativeUi.rsg>
       
    29 #include <akntoolbar.h>
       
    30 #include <aknbutton.h>
       
    31 #include <AknsUtils.h>
       
    32 #include <gulicon.h>
       
    33 #include <e32property.h>
       
    34 #include <aknappui.h>
       
    35 
       
    36 #include "centralrepository.h"
       
    37 
       
    38 
       
    39 #include "debug.h"
       
    40 
       
    41 using namespace AiNativeUiController;
       
    42 
       
    43 // Action to send to framework
       
    44 _LIT( KOpenAppByIndex,              "Shortcut/LaunchByIndex(%d)" );
       
    45 _LIT( KOpenAppByIndexAlternate,     "Shortcut/LaunchByIndexAlternate(%d)");
       
    46 
       
    47 const TInt KOpenAppByIndexParameterLength = 2;
       
    48 
       
    49 const TInt KFirstButton  = 0;
       
    50 const TInt KSecondButton = 1;
       
    51 const TInt KThirdButton  = 2;
       
    52 
       
    53 const TInt KRadix = 10;
       
    54 
       
    55 static TInt IndexLength( TInt aIndex )
       
    56     {
       
    57     TInt length = 0;
       
    58 
       
    59     if ( aIndex < 0 )
       
    60         {
       
    61         ++length;
       
    62         }
       
    63 
       
    64     do
       
    65         {
       
    66         aIndex /= KRadix;
       
    67         ++length;
       
    68         }
       
    69     while ( aIndex != 0 );
       
    70 
       
    71     return length;
       
    72     }
       
    73 
       
    74 static HBufC* CreateEventStringL( const TDesC& aFrom, TInt aEventIndex )
       
    75     {
       
    76     HBufC* buffer = NULL;
       
    77 
       
    78     TInt bufferLength = aFrom.Length() -
       
    79                         KOpenAppByIndexParameterLength +
       
    80                         IndexLength( aEventIndex );
       
    81 
       
    82     buffer = HBufC::NewL(bufferLength);
       
    83 
       
    84     TPtr bufferPtr = buffer->Des();
       
    85     bufferPtr.Format( aFrom, aEventIndex );
       
    86 
       
    87     return buffer;
       
    88     }
       
    89 
       
    90 // ========== MEMBER FUNCTIONS ================================================
       
    91 
       
    92 CAiToolbarRenderer::CAiToolbarRenderer( MAiFwEventHandler& aAiFwEventHandler, CAknToolbar& aToolbar )
       
    93     : iToolbar( aToolbar ),
       
    94     iAiFwEventHandler( aAiFwEventHandler )
       
    95 
       
    96     {
       
    97     // no implementation required
       
    98     }
       
    99 
       
   100 void CAiToolbarRenderer::ConstructL( )
       
   101     {
       
   102     iToolbarEnabled = EFalse;
       
   103 
       
   104     TInt err = KErrNone;
       
   105 
       
   106     CRepository *cr = CRepository::NewLC( TUid::Uid( KCRUidActiveIdleLV ) );
       
   107     err = cr->Get( KAITouchToolbarVisible, iToolbarEnabled );
       
   108     CleanupStack::PopAndDestroy( cr );
       
   109     __PRINT (__DBG_FORMAT("XAI: Toolbar value in cenrep %d err = %d"), iToolbarEnabled , err);
       
   110 
       
   111     // Hide the toolbar because it is disabled via cenrep
       
   112     if ( err != KErrNone || !iToolbarEnabled )
       
   113         {
       
   114         iToolbar.SetToolbarObserver( NULL );
       
   115         iToolbar.SetToolbarVisibility( EFalse, EFalse );
       
   116         }
       
   117     else
       
   118         {
       
   119         iToolbar.SetToolbarObserver( this );
       
   120         iToolbar.SetToolbarVisibility( ETrue, EFalse );
       
   121         }
       
   122 
       
   123     }
       
   124 
       
   125 CAiToolbarRenderer* CAiToolbarRenderer::NewLC( MAiFwEventHandler& aAiFwEventHandler, CAknToolbar& aToolbar )
       
   126     {
       
   127     CAiToolbarRenderer* self = new (ELeave) CAiToolbarRenderer( aAiFwEventHandler, aToolbar );
       
   128     CleanupStack::PushL( self );
       
   129     self->ConstructL( );
       
   130     return self;
       
   131     }
       
   132 
       
   133 CAiToolbarRenderer* CAiToolbarRenderer::NewL( MAiFwEventHandler& aAiFwEventHandler, CAknToolbar& aToolbar )
       
   134     {
       
   135     CAiToolbarRenderer* self = CAiToolbarRenderer::NewLC( aAiFwEventHandler, aToolbar );
       
   136     CleanupStack::Pop(); // self
       
   137     return self;
       
   138     }
       
   139 
       
   140 CAiToolbarRenderer::~CAiToolbarRenderer()
       
   141     {
       
   142     // We don't want anymore events from toolbar
       
   143     iToolbar.SetToolbarObserver( NULL );
       
   144     iButtons.ResetAndDestroy();
       
   145     }
       
   146 
       
   147 void CAiToolbarRenderer::DynInitToolbarL( TInt /*aResourceId*/, CAknToolbar* /*aToolbar*/ )
       
   148     {
       
   149     // do nothing
       
   150     }
       
   151 
       
   152 void CAiToolbarRenderer::OfferToolbarEventL( TInt aCommand )
       
   153     {
       
   154     TInt modifiers = iToolbar.EventModifiers();
       
   155 
       
   156 
       
   157     if ( aCommand >= 0 && aCommand < iButtons.Count())
       
   158         {
       
   159         CAiToolbarButton* button = iButtons[aCommand];
       
   160         if ( button )
       
   161             {
       
   162 
       
   163             HBufC *event = NULL;
       
   164             if ( modifiers == CAknToolbar::ELongPress )
       
   165                 {
       
   166                 event = CreateEventStringL( KOpenAppByIndexAlternate, button->ShortcutId() );
       
   167                 CleanupStack::PushL( event );
       
   168                 }
       
   169             else
       
   170                 {
       
   171                 event = CreateEventStringL( KOpenAppByIndex, button->ShortcutId() );
       
   172                 CleanupStack::PushL( event );
       
   173                 }
       
   174             iAiFwEventHandler.HandlePluginEvent( *event );
       
   175             CleanupStack::PopAndDestroy( event );
       
   176             }
       
   177         }
       
   178     }
       
   179 
       
   180 void CAiToolbarRenderer::DoPublishL( MAiPropertyExtension& aPlugin,
       
   181                                      TInt aContent,
       
   182                                      const TDesC16& aText,
       
   183                                      TInt aIndex )
       
   184     {
       
   185     // Only interested on certain publish events
       
   186     if (  aPlugin.PublisherInfoL()->iUid == KShortcutPluginUid &&
       
   187           aContent == EAiScutContentShortcutToolbarCaption &&
       
   188         ( aIndex == EAiScutToolbarFirst ||
       
   189           aIndex == EAiScutToolbarSecond ||
       
   190           aIndex == EAiScutToolbarThird )
       
   191         )
       
   192         {
       
   193         CAiToolbarButton* button = ButtonL( aIndex );
       
   194         if ( button )
       
   195             {
       
   196             button->SetTooltipL(aText);
       
   197             iCommitted = EFalse;
       
   198             }
       
   199         }
       
   200     }
       
   201 
       
   202 void CAiToolbarRenderer::DoPublishL( MAiPropertyExtension& aPlugin,
       
   203                                      TInt aContent,
       
   204                                      const TDesC8& aBuf,
       
   205                                      TInt aIndex )
       
   206     {
       
   207     // Only interested on certain publish events
       
   208     if (  aPlugin.PublisherInfoL()->iUid == KShortcutPluginUid &&
       
   209           aContent == EAiScutContentShortcutToolbarIcon &&
       
   210         ( aIndex == EAiScutToolbarFirst ||
       
   211           aIndex == EAiScutToolbarSecond ||
       
   212           aIndex == EAiScutToolbarThird )
       
   213         )
       
   214         {
       
   215         CGulIcon* icon = MAiContentObserver::UnpackPtr<CGulIcon>( aBuf );
       
   216         CAiToolbarButton* button = NULL;
       
   217         if ( !icon )
       
   218             {
       
   219             User::Leave(KErrArgument);
       
   220             }
       
   221         // Replace the old button with a new button
       
   222         button = ButtonL( aIndex );
       
   223         if ( button )
       
   224             {
       
   225             button->SetIcon(icon);
       
   226             iCommitted = EFalse;
       
   227             }
       
   228         // icons ownership is transferred to us
       
   229         // so we need to delete the icon in error situations
       
   230         else
       
   231             {
       
   232             delete icon;
       
   233             }
       
   234         }
       
   235     else
       
   236         {
       
   237         // We have received stuff we cannot publish.
       
   238         User::Leave(KErrNotFound);
       
   239         }
       
   240     }
       
   241 
       
   242 CAiToolbarButton* CAiToolbarRenderer::ButtonL( TInt aShortcutId )
       
   243     {
       
   244     TInt index = KErrNotFound;
       
   245     CAiToolbarButton *button = NULL;
       
   246     switch ( aShortcutId )
       
   247         {
       
   248         case EAiScutToolbarFirst:
       
   249             index = KFirstButton;
       
   250             break;
       
   251         case EAiScutToolbarSecond:
       
   252             index = KSecondButton;
       
   253             break;
       
   254         case EAiScutToolbarThird:
       
   255             index = KThirdButton;
       
   256             break;
       
   257         default:
       
   258             break;
       
   259         }
       
   260 
       
   261     if ( index == KErrNotFound )
       
   262         {
       
   263         User::Leave(KErrNotFound);
       
   264         }
       
   265     // No button yet => Create new
       
   266     if ( index >= iButtons.Count() )
       
   267         {
       
   268         button = CAiToolbarButton::NewL();
       
   269         button->SetShortcutId( aShortcutId );
       
   270         iButtons.Append( button );
       
   271         }
       
   272 
       
   273     // Use the old button
       
   274     else if( index >= KFirstButton && index < iButtons.Count() )
       
   275         {
       
   276         button = iButtons[index];
       
   277         button->SetShortcutId( aShortcutId );
       
   278         }
       
   279     return button;
       
   280     }
       
   281 
       
   282 
       
   283 void CAiToolbarRenderer::TransactionCommittedL()
       
   284     {
       
   285     if ( !iCommitted )
       
   286         {
       
   287         for(TInt i = 0; i < iButtons.Count(); i++)
       
   288             {
       
   289             CAiToolbarButton *button = iButtons[i];
       
   290             if ( button && button->Icon())
       
   291                 {
       
   292                 // remove the old button
       
   293                 iToolbar.RemoveItem(i);
       
   294                 // and add new one
       
   295                 // transfers ownership of button->ButtonL() to toolbar
       
   296                 iToolbar.AddItemL( button->ButtonL(), EAknCtButton, i, 0, i );
       
   297                 }
       
   298             }
       
   299          iCommitted = ETrue;
       
   300         }
       
   301     }
       
   302 
       
   303 
       
   304 // End of File.