ui/commandhandlers/commoncommandhandlers/src/glxcommandhandlernewmedia.cpp
changeset 24 99ad1390cd33
parent 23 74c9f037fd5d
child 26 c499df2dbb33
equal deleted inserted replaced
23:74c9f037fd5d 24:99ad1390cd33
    18 #include <mpxcollectionpath.h>
    18 #include <mpxcollectionpath.h>
    19 #include <mglxmedialist.h>
    19 #include <mglxmedialist.h>
    20 #include <glxcommandfactory.h>
    20 #include <glxcommandfactory.h>
    21 #include <glxcommandhandlernewmedia.h>
    21 #include <glxcommandhandlernewmedia.h>
    22 #include <glxcommandhandlers.hrh>
    22 #include <glxcommandhandlers.hrh>
       
    23 #include <glxattributecontext.h>
       
    24 #include <glxattributeretriever.h>
       
    25 #include <glxfetchcontextremover.h>
       
    26 #include <glxcollectionpluginalbums.hrh>
    23 
    27 
    24 #include <hbinputdialog.h>
    28 #include <hbinputdialog.h>
    25 
    29 #include <hblabel.h>
    26 GlxCommandHandlerNewMedia::GlxCommandHandlerNewMedia():iNewMediaCreationError(KErrNone) , iNewMediaItemTitle(0)
    30 #include <hbmessagebox.h>
    27 {
    31 #include "OstTraceDefinitions.h"
    28     qDebug("GlxCommandHandlerNewMedia::GlxCommandHandlerNewMedia() ");
    32 #ifdef OST_TRACE_COMPILER_IN_USE
       
    33 #include "glxcommandhandlernewmediaTraces.h"
       
    34 #endif
       
    35 #include <hbaction.h>
       
    36 
       
    37 GlxTextInputDialog::GlxTextInputDialog()
       
    38     {
       
    39     }
       
    40 
       
    41 GlxTextInputDialog::~GlxTextInputDialog()
       
    42     {
       
    43     }
       
    44 
       
    45 QString GlxTextInputDialog::getText(const QString &label,
       
    46         const QString &text, bool *ok)
       
    47     {
       
    48     mDialog = new HbInputDialog();
       
    49     mDialog->setPromptText(label);
       
    50     mDialog->setInputMode(HbInputDialog::TextInput);
       
    51     mDialog->setValue(text);
       
    52     connect(mDialog->lineEdit(0), SIGNAL( textChanged (const QString &) ),
       
    53             this, SLOT( textChanged (const QString &)));
       
    54     HbAction* action = mDialog->exec();
       
    55     QString retText = NULL;
       
    56     if (action == mDialog->secondaryAction())
       
    57         { //Cancel was pressed
       
    58         if (ok)
       
    59             {
       
    60             *ok = false;
       
    61             }
       
    62         }
       
    63     else
       
    64         { //OK was pressed
       
    65         if (ok)
       
    66             {
       
    67             *ok = true;
       
    68             }
       
    69         retText = mDialog->value().toString().trimmed();
       
    70         }
       
    71     disconnect(mDialog->lineEdit(0), SIGNAL( textChanged (const QString &) ),
       
    72             this, SLOT( textChanged (const QString &)));
       
    73     delete mDialog;
       
    74     mDialog = NULL;
       
    75     return retText;
       
    76     }
       
    77 
       
    78 void GlxTextInputDialog::textChanged(const QString &text)
       
    79     {
       
    80     if (text.trimmed().isEmpty())
       
    81         {
       
    82         mDialog->primaryAction()->setEnabled(false);
       
    83         }
       
    84     else
       
    85         {
       
    86         mDialog->primaryAction()->setEnabled(true);
       
    87         }
       
    88     }
       
    89 
       
    90 
       
    91 GlxCommandHandlerNewMedia::GlxCommandHandlerNewMedia() :
       
    92     iNewMediaCreationError(KErrNone), iNewMediaItemTitle(0) , mShowConfirmation(false)
       
    93     {
       
    94     OstTraceFunctionEntry0( GLXCOMMANDHANDLERNEWMEDIA_GLXCOMMANDHANDLERNEWMEDIA_ENTRY );
    29     iSchedulerWait = new (ELeave) CActiveSchedulerWait();
    95     iSchedulerWait = new (ELeave) CActiveSchedulerWait();
    30 }
    96     OstTraceFunctionExit0( GLXCOMMANDHANDLERNEWMEDIA_GLXCOMMANDHANDLERNEWMEDIA_EXIT );
       
    97     }
    31 
    98 
    32 GlxCommandHandlerNewMedia::~GlxCommandHandlerNewMedia()
    99 GlxCommandHandlerNewMedia::~GlxCommandHandlerNewMedia()
    33 {
   100     {
    34     qDebug("GlxCommandHandlerNewMedia::~GlxCommandHandlerNewMedia() ");
   101     OstTraceFunctionEntry0( DUP1_GLXCOMMANDHANDLERNEWMEDIA_GLXCOMMANDHANDLERNEWMEDIA_ENTRY );
       
   102     if (iSchedulerWait && iSchedulerWait->IsStarted())
       
   103         {
       
   104         iSchedulerWait->AsyncStop();
       
   105         }
    35     delete iSchedulerWait;
   106     delete iSchedulerWait;
    36     delete iNewMediaItemTitle;
   107     delete iNewMediaItemTitle;
    37 }
   108     OstTraceFunctionExit0( DUP1_GLXCOMMANDHANDLERNEWMEDIA_GLXCOMMANDHANDLERNEWMEDIA_EXIT );
    38 
   109     }
    39 CMPXCommand* GlxCommandHandlerNewMedia::CreateCommandL(TInt aCommandId, MGlxMediaList& aMediaList, TBool& aConsume) const 
   110 
    40 {
   111 CMPXCommand* GlxCommandHandlerNewMedia::CreateCommandL(TInt /*aCommandId*/,
    41     qDebug("GlxCommandHandlerNewMedia::CreateCommandL");
   112         MGlxMediaList& aMediaList, TBool& /*aConsume*/) const
    42     CMPXCollectionPath* path = aMediaList.PathLC( NGlxListDefs::EPathParent );
   113     {
       
   114     OstTraceFunctionEntry0( GLXCOMMANDHANDLERNEWMEDIA_CREATECOMMANDL_ENTRY );
       
   115     CMPXCollectionPath* path = aMediaList.PathLC(NGlxListDefs::EPathParent);
    43     CMPXCommand* command = NULL;
   116     CMPXCommand* command = NULL;
    44     
   117    
       
   118 
    45     _LIT(KName, "TEST");
   119     _LIT(KName, "TEST");
    46     HBufC* newMediaItemTitle = HBufC::NewL(40);
   120     HBufC* newMediaItemTitle = HBufC::NewL(40);
    47     TPtr newMediaItemTitleDes = newMediaItemTitle->Des();
   121     TPtr newMediaItemTitleDes = newMediaItemTitle->Des();
    48     newMediaItemTitleDes.Append(KName);
   122     newMediaItemTitleDes.Append(KName);
    49     
   123 
       
   124 
    50     delete iNewMediaItemTitle;
   125     delete iNewMediaItemTitle;
    51     iNewMediaItemTitle= NULL;
   126     iNewMediaItemTitle = NULL;
    52     iNewMediaItemTitle = newMediaItemTitle;
   127     iNewMediaItemTitle = newMediaItemTitle;
    53 
   128 
    54     QString title("NEW MEDIA");
   129     QString title("NEW MEDIA");
    55     QString mainPane = QString("Album %1").arg(qrand());
   130     QString mainPane = GenerateNewMediaItemTitleL("Album",aMediaList);
    56     bool ok = false;
   131     bool ok = false;
    57 
   132 	QString mediaTitle = NULL;
    58     QString mediaTitle = HbInputDialog::getText(title,  mainPane,  &ok);
   133     GlxTextInputDialog* dlg = new GlxTextInputDialog();
    59     qDebug("GlxCommandHandlerNewMedia::CreateCommandL %d", ok);
   134     mediaTitle = dlg->getText(title, mainPane, &ok);
    60     
   135     delete dlg;
    61     if(ok == true) {
   136     iNewMediaCreationError = KErrNone;
    62         TPtr newMediaItemTitleDes = iNewMediaItemTitle->Des();
   137     if (ok == true)
    63         newMediaItemTitleDes = (reinterpret_cast<const TUint16*>(mediaTitle.utf16()));
   138         {
    64         command = TGlxCommandFactory::AddContainerCommandLC(*iNewMediaItemTitle, path->Id(0));
   139         
       
   140         TPtrC16 newMediaItemTitleDes
       
   141                 = (reinterpret_cast<const TUint16*> (mediaTitle.utf16()));
       
   142         
       
   143         delete iNewMediaItemTitle;
       
   144         iNewMediaItemTitle = NULL;        
       
   145         iNewMediaItemTitle = newMediaItemTitleDes.Alloc();
       
   146         
       
   147         command = TGlxCommandFactory::AddContainerCommandLC(
       
   148                 *iNewMediaItemTitle, path->Id(0));
    65         CleanupStack::Pop(command);
   149         CleanupStack::Pop(command);
    66     }
   150         }
    67     else {
   151     else
    68     	iNewMediaCreationError = KErrCancel;
   152         {
    69     }
   153         iNewMediaCreationError = KErrCancel;
    70     	
   154         }
       
   155 
    71     CleanupStack::PopAndDestroy(path);
   156     CleanupStack::PopAndDestroy(path);
       
   157     OstTraceFunctionExit0( GLXCOMMANDHANDLERNEWMEDIA_CREATECOMMANDL_EXIT );
    72     return command;
   158     return command;
    73 }
   159     }
    74 
   160 
    75 
   161 
    76 TInt GlxCommandHandlerNewMedia::ExecuteLD(TGlxMediaId& aNewMediaId)
   162 TInt GlxCommandHandlerNewMedia::ExecuteLD(TGlxMediaId& aNewMediaId)
    77 {
   163     {
    78     qDebug("GlxCommandHandlerNewMedia::ExecuteLD() ");
   164     OstTraceFunctionEntry0( GLXCOMMANDHANDLERNEWMEDIA_EXECUTELD_ENTRY );
    79     GlxMpxCommandHandler::executeCommand( EGlxCmdAddMedia,KGlxAlbumsMediaId);
   165     GlxMpxCommandHandler::executeCommand(EGlxCmdAddMedia, KGlxCollectionPluginAlbumsImplementationUid);
    80 
   166     mShowConfirmation = true;
    81     if (iNewMediaCreationError == KErrNone) {
   167     if (iNewMediaCreationError == KErrNone)
       
   168         {
    82         // The user pressed OK on the dialog. We need to wait for DoHandleCommandComplete()	
   169         // The user pressed OK on the dialog. We need to wait for DoHandleCommandComplete()	
    83        iSchedulerWait->Start();
   170         iSchedulerWait->Start();
    84 		
   171 
    85        if (iNewMediaCreationError == KErrNone) {
   172         if (iNewMediaCreationError == KErrNone)
       
   173             {
    86             aNewMediaId = iNewMediaId;
   174             aNewMediaId = iNewMediaId;
    87         }
   175             }
    88     }
   176         }
    89 	
   177 
    90     TInt error = iNewMediaCreationError;
   178     TInt error = iNewMediaCreationError;
       
   179     OstTraceFunctionExit0( GLXCOMMANDHANDLERNEWMEDIA_EXECUTELD_EXIT );
    91     return error;
   180     return error;
    92 }
   181     }
    93 
   182 
    94 // -----------------------------------------------------------------------------
   183 // -----------------------------------------------------------------------------
    95 // CGlxCommandHandlerNewMedia::DoHandleCommandCompleteL
   184 // CGlxCommandHandlerNewMedia::DoHandleCommandCompleteL
    96 // -----------------------------------------------------------------------------
   185 // -----------------------------------------------------------------------------
    97 //	
   186 //	
    98 void GlxCommandHandlerNewMedia::DoHandleCommandCompleteL(TAny* /*aSessionId*/, CMPXCommand* aCommandResult, 
   187 void GlxCommandHandlerNewMedia::DoHandleCommandCompleteL(
    99             							TInt aError, MGlxMediaList* /*aList*/)
   188         TAny* /*aSessionId*/, CMPXCommand* aCommandResult, TInt aError,
   100 {
   189         MGlxMediaList* /*aList*/)
   101     qDebug("GlxCommandHandlerNewMedia::DoHandleCommandCompleteL() ");
   190     {
   102     if (aError == KErrNone && aCommandResult && aCommandResult->IsSupported(KMPXMediaGeneralId)) {	
   191     OstTraceFunctionEntry0( GLXCOMMANDHANDLERNEWMEDIA_DOHANDLECOMMANDCOMPLETEL_ENTRY );
   103         iNewMediaId = TGlxMediaId(aCommandResult->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
   192     if (aError == KErrNone && aCommandResult && aCommandResult->IsSupported(
   104     }	
   193             KMPXMediaGeneralId))
   105 
   194         {
   106     if (iSchedulerWait && aError != KErrAlreadyExists ) {
   195         iNewMediaId = TGlxMediaId(aCommandResult->ValueTObjectL<TMPXItemId> (
       
   196                 KMPXMediaGeneralId));
       
   197         }
       
   198     if (iSchedulerWait && iSchedulerWait->IsStarted())
       
   199         {
   107         // if iSchedulerWait exists then we know the command is being executed from
   200         // if iSchedulerWait exists then we know the command is being executed from
   108         // the ExecuteLD() method.
   201         // the ExecuteLD() method.
   109         iNewMediaCreationError = aError;
   202         iNewMediaCreationError = aError;
   110         iSchedulerWait->AsyncStop();
   203         iSchedulerWait->AsyncStop();
   111     }
   204         }
   112 }
   205     
       
   206     OstTraceFunctionExit0( GLXCOMMANDHANDLERNEWMEDIA_DOHANDLECOMMANDCOMPLETEL_EXIT );
       
   207     }
   113 
   208 
   114 // -----------------------------------------------------------------------------
   209 // -----------------------------------------------------------------------------
   115 // HandleItemAddedL
   210 // HandleItemAddedL
   116 // -----------------------------------------------------------------------------
   211 // -----------------------------------------------------------------------------
   117 //  
   212 //  
   118 void GlxCommandHandlerNewMedia::HandleItemAddedL(TInt aStartIndex, TInt aEndIndex, MGlxMediaList* aList)
   213 void GlxCommandHandlerNewMedia::HandleItemAddedL(TInt aStartIndex,
   119 {
   214         TInt aEndIndex, MGlxMediaList* aList)
   120     qDebug("GlxCommandHandlerNewMedia::HandleItemAddedL() ");
   215     {
   121     if(/*aList == &MediaList() && */iNewMediaId != KGlxCollectionRootId) {
   216     OstTraceFunctionEntry0( GLXCOMMANDHANDLERNEWMEDIA_HANDLEITEMADDEDL_ENTRY );
   122          for (TInt i = aStartIndex; i <= aEndIndex; i++) {
   217     if (iNewMediaId != KGlxCollectionRootId)
   123             if (aList->Item(i).Id() == iNewMediaId) {
   218         {
   124                 qDebug("GlxCommandHandlerNewMedia::HandleItemAddedL()::SetFocusL index= %d ",i);
   219         for (TInt i = aStartIndex; i <= aEndIndex; i++)
       
   220             {
       
   221             if (aList->Item(i).Id() == iNewMediaId)
       
   222                 {
   125                 aList->SetFocusL(NGlxListDefs::EAbsolute, i); // calls CGlxCommandHandlerNewMedia::SetFocusL asynchronously
   223                 aList->SetFocusL(NGlxListDefs::EAbsolute, i); // calls CGlxCommandHandlerNewMedia::SetFocusL asynchronously
   126                 if (iSchedulerWait /*&& aError != KErrAlreadyExists*/ ) {
   224                 if (iSchedulerWait && iSchedulerWait->IsStarted() )
       
   225                     {
   127                     // if iSchedulerWait exists then we know the command is being executed from
   226                     // if iSchedulerWait exists then we know the command is being executed from
   128                     // the ExecuteLD() method.
   227                     // the ExecuteLD() method.
   129                     iNewMediaCreationError = KErrNone;
   228                     iNewMediaCreationError = KErrNone;
   130                     iSchedulerWait->AsyncStop();
   229                     iSchedulerWait->AsyncStop();
       
   230                     }
       
   231 
       
   232                 break;
   131                 }
   233                 }
   132 
   234             }
   133                 break;
   235         }
   134             }
   236     OstTraceFunctionExit0( GLXCOMMANDHANDLERNEWMEDIA_HANDLEITEMADDEDL_EXIT );
   135         }
   237     }
   136     }
   238 
       
   239 void GlxCommandHandlerNewMedia::HandleError(TInt /*aError*/)
       
   240     {
       
   241     OstTraceFunctionEntry0( GLXCOMMANDHANDLERNEWMEDIA_HANDLEERROR_ENTRY );
       
   242     OstTraceFunctionExit0( GLXCOMMANDHANDLERNEWMEDIA_HANDLEERROR_EXIT );
       
   243     }
       
   244 
       
   245 void GlxCommandHandlerNewMedia::HandleErrorL(TInt aErrorCode)
       
   246     {
       
   247     OstTraceFunctionEntry0( GLXCOMMANDHANDLERNEWMEDIA_HANDLEERRORL_ENTRY );
       
   248     if (aErrorCode == KErrAlreadyExists && !mShowConfirmation)
       
   249         {
       
   250         HbMessageBox::warning("Album Already Exist!!!" );
       
   251         }
       
   252     OstTraceFunctionExit0( GLXCOMMANDHANDLERNEWMEDIA_HANDLEERRORL_EXIT );
       
   253     }
       
   254 
       
   255 QString GlxCommandHandlerNewMedia::GenerateNewMediaItemTitleL(
       
   256         QString newMediaTilte, MGlxMediaList& aMediaList) const
       
   257     {
       
   258     TGlxSequentialIterator iter;
       
   259     CGlxAttributeContext* attributeContext =
       
   260             new (ELeave) CGlxAttributeContext(&iter);
       
   261     CleanupStack::PushL(attributeContext);
       
   262     attributeContext->AddAttributeL(KMPXMediaGeneralTitle);
       
   263     aMediaList.AddContextL(attributeContext, KGlxFetchContextPriorityBlocking);
       
   264     TGlxFetchContextRemover contextRemover(attributeContext, aMediaList);
       
   265     // put to cleanupstack as cleanupstack is emptied before stack objects
       
   266     // are deleted
       
   267     CleanupClosePushL(contextRemover);
       
   268     User::LeaveIfError(GlxAttributeRetriever::RetrieveL(*attributeContext,
       
   269             aMediaList, ETrue));
       
   270     // context off the list
       
   271     CleanupStack::PopAndDestroy(&contextRemover);
       
   272     CleanupStack::PopAndDestroy(attributeContext);
       
   273 
       
   274     RArray<TInt> numbers;
       
   275     CleanupClosePushL(numbers);
       
   276 
       
   277     TInt count = aMediaList.Count();
       
   278     for (TInt i = 0; i < count; i++)
       
   279         {
       
   280         TGlxMedia item = aMediaList.Item(i);
       
   281         const CGlxMedia* media = item.Properties();
       
   282         if (media)
       
   283             {
       
   284             const TDesC& titleDesc = media->ValueText(KMPXMediaGeneralTitle);
       
   285             QString title = QString::fromUtf16(titleDesc.Ptr(),
       
   286                     titleDesc.Length());
       
   287 
       
   288             TInt length = newMediaTilte.length();
       
   289             if (title.startsWith(newMediaTilte, Qt::CaseSensitive))
       
   290                 {
       
   291                 if (length == title.length())
       
   292                     {
       
   293                     numbers.InsertInOrder(0); // special case
       
   294                     }
       
   295                 else if (title.length() > length)
       
   296                     {
       
   297                     QString num = title.mid(length);
       
   298                     bool ok = false;
       
   299                     TInt dec = num.toInt(&ok);
       
   300                     if (ok)
       
   301                         {
       
   302                         numbers.InsertInOrder(dec);
       
   303                         }
       
   304                     }
       
   305                 }
       
   306             }
       
   307         }
       
   308 
       
   309     TInt nextNumber = 0;
       
   310     count = numbers.Count();
       
   311     for (TInt i = 0; i < count; i++)
       
   312         {
       
   313         if (numbers[i] == nextNumber)
       
   314             {
       
   315             nextNumber++;
       
   316             }
       
   317         else
       
   318             {
       
   319             break;
       
   320             }
       
   321         }
       
   322 
       
   323     CleanupStack::PopAndDestroy(&numbers);
       
   324 
       
   325     QString newMediaItemTitle = newMediaTilte;
       
   326 
       
   327     if (nextNumber > 0)
       
   328         {
       
   329         newMediaItemTitle.append(QString("%1").arg(nextNumber));
       
   330         }
       
   331     return newMediaItemTitle;
       
   332 
   137 }
   333 }
       
   334 
       
   335 QString GlxCommandHandlerNewMedia::CompletionTextL() const
       
   336     {
       
   337     if (!mShowConfirmation)
       
   338         {
       
   339         return QString("Album added");
       
   340         }
       
   341     return QString();
       
   342     }
       
   343 
       
   344 QString GlxCommandHandlerNewMedia::ProgressTextL() const
       
   345     {
       
   346     if (!mShowConfirmation)
       
   347         {
       
   348         return QString("Adding album...");
       
   349         }
       
   350     return QString();
       
   351     }
       
   352