Task: Local Content

This tutorial, based on the LocalContent sample delivered with the bada SDK in \<BADA_SDK_HOME>\Samples\LocalContent, demonstrates how you can use the Osp::Content namespace to search for content on your device. For more information on the sample functionality, see LocalContent Sample Overview.

This tutorial builds upon the elements described in Searching Device Content, and consists of the following parts:

The tutorial does not cover information on general bada C++ application structure or on using the UI elements; for those issues, see App Tutorial and UI Tutorial.

The sample is a fully functional content search application which searches for local files on the device, and also allows you to add content info for individual media files.

The application includes the following UI controls:

In this sample, the forms, pop-up, and edit fields and button are created with UI Builder.

Figure: Content search forms

Content search forms

The content information is placed and maintained in the device's content database instead of the individual files, so the images and other content files themselves are not modified when the content information is changed.

Tip

When using the Emulator to run the application, note that it searches for the content from the \<BADA_SDK_HOME>\Model\<Device_model>\Emulator\FS\Media directory.

Before running the application, you must copy the relevant files to that location, as the search index is created in the Emulator when it is launched.

The LocalContent sample application uses the bada Form Based Application template, which contains a preset Osp::Ui::Controls::Form class. Most methods used in the sample remain as they were created in the project template.

To Initialize the Application

  1. DetailForm.h Header File
    1. The DetailForm class declares the Osp::Ui::IActionEventListener interface to be able to manage user actions related to the footer items.

      class DetailForm:
         public Osp::Ui::Controls::Form,
         public Osp::Ui::IActionEventListener,
         public Osp::Ui::Controls::IFormBackEventListener
      {
      
    2. An event handler is also declared for the IActionEventListener interface, as is the LoadContentInfo() method for accessing the content details.

         public:
            result OnInitializing(void);
            result OnTerminating(void);
            void OnActionPerformed(const Osp::Ui::Control& source, int actionId);
            void OnFormBackRequested(Osp::Ui::Controls::Form& source);
      
            void LoadContentInfo(Osp::Content::ContentSearchResult* pSearchResult);
            void SetPreviousForm(Osp::Ui::Controls::Form* preForm);
      
         protected:
            result init(void);
            void UpdateContent(void);
            result DeleteContent(void);
            void GetPrevForm(void);
      };
      

The query string is retrieved in the MainForm.cpp file. The search functionality is implemented in the ContentSearchSampleImpl.cpp file.

  1. Searching for Local Content
    1. In the SearchContent() method, the first search-specific features start to appear. The first part of the method retrieves the content for the search Osp::Base::String instance.

      result MainForm::SearchContent()
      {
         result r = E_SUCCESS;
         String queryString;
         String popStr;
         Frame *pFrame = null;
      
         // Get query string
         queryString = __pEFContentName->GetText();
    2. The SearchContent() method of the ContentSearchSampleImpl.cpp file is called to perform the actual search.

         // Execute the query
         r = __pContentSearchImpl->SearchContent(__contentType, queryString);
      	
         // Display search result list
         pFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
         pFrame->SetCurrentForm(*__pSearchResultForm);
         __pSearchResultForm->DisplayResultList(__pContentSearchImpl);
      }
    3. When the SearchContent() method is called, it clears the previous search results using the initResultList() method.

      result ContentSearchSampleImpl::SearchContent(Osp::Content::ContentType contentType, 
                                                    Osp::Base::String& query)
      {
         result r = E_SUCCESS;
         ContentSearch contentSearch;
         String sortColumn = L"";
         String strQuery;
         int totalPage = 0;
         int totalCount = 0;
         int page = 1;
      
         // Clear the previous result
         initResultList();
    4. The actual search is implemented with the SearchN() method. After receiving the search results, the application creates a list for the search results and populates it. The large number of attributes in the searchN() method may look intimidating, but all of them were initialized at the beginning of the SearchContent() method.

         __contentType = contentType;
         __queryString = query;
      
         r = contentSearch.Construct(__contentType);
      
         strQuery = makeQuery(query);
      
         // Execute
         __pSearchResultList = contentSearch.SearchN(page, 
                                                     MAX_CONTENTSEARCH_COUNTPERPAGE, 
                                                     totalPage, totalCount, strQuery, 
                                                     sortColumn, SORT_ORDER_NONE);
      }
  2. Handling Content Type Selection

    The CreateItem() method is called automatically when the content type list view is created.

    This method creates an instance of the Osp::Ui::Controls::CustomItem class to create list items to be added to the list view. Once all the content types have been defined, the AddElement() method is used to populate the list view.

    Osp::Ui::Controls::ListItemBase* MainForm::CreateItem(int index, int itemWidth)
    {
       String contentTypeName;
       ListAnnexStyle style = LIST_ANNEX_STYLE_MARK;
       CustomItem* pItem = new CustomItem();
       pItem->Construct(Osp::Graphics::Dimension(itemWidth, MAX_LISTVIEW_ITEM_SIZE), style);
    
       switch (index)
       {
          case CONTENT_TYPE_IMAGE:
             contentTypeName = L"Image";
             break;
          case CONTENT_TYPE_AUDIO:
             contentTypeName = L"Audio";
             break;
          case CONTENT_TYPE_VIDEO:
             contentTypeName = L"Video";
             break;
          case CONTENT_TYPE_OTHER:
             contentTypeName = L"Other";
             break;
          default:
             break;
       }
    
       pItem->AddElement(Rectangle(20, 0, itemWidth - 20, MAX_LISTVIEW_ITEM_SIZE), 100, 
                         contentTypeName);
    
       return pItem;
    }
  3. Displaying the Search Result

    The SearchResultForm.cpp file is called from the MainForm class to display the search results. The DisplayResultList() method updates the list view with the search results using the UpdateList() method.

    The UpdateList() method invokes the Osp::Ui::Controls::ListItemBase* SearchResultForm::CreateItem() method to add clear the contents of the existing list view and to add the search result elements.

    result SearchResultForm::DisplayResultList(ContentSearchSampleImpl* pContentSearchImpl)
    {
       result r = E_SUCCESS;
       __pContentSearchImpl = pContentSearchImpl;
       r = __pListView->UpdateList();
       Draw();
       Show();
    
       return r;
    }

To Manage Content Info

Content info handling is implemented in the DetailForm.cpp file. From a UI point of view, the DetailForm form is much simpler than the MainForm form, but there are many more content-specific actions going on.

  1. Loading Content Info

    When the DetailForm form is called, the relevant content for the selected file must be loaded from the device's content database. If the file contains no content info, all the EditField instances in this form are empty at first.

    void DetailForm::LoadContentInfo(Osp::Content::ContentSearchResult* pSearchResult)
    {
       String tmpString;
       String ch;
       int index = 0;
       result r = E_SUCCESS;
    
       __pFooter->SetItemSelected(0);
       // If you want to get content info, convert to XXXXMetaContentInfo 
       // after the pSearchResult->GetContentType() check
       __pContentInfo = pSearchResult->GetContentInfo();
    
       tmpString = __pContentInfo->GetContentPath();
       __strOrgAuthor = __pContentInfo->GetAuthor();
       __strOrgKeyword = __pContentInfo->GetKeyword();
    
       // Eliminate a directory name from the ContentPath
       ch.Append("/");
    
       r = tmpString.LastIndexOf(ch, tmpString.GetLength() - 1, index);
       if (r == E_SUCCESS && index > 0)
       {
          r = tmpString.SubString(index + 1, __strOrgCntName);
       }
       else
       {
          __strOrgCntName = tmpString;
       }
    
       __pLblContentName->SetText(__strOrgCntName);
       __pEFAuthor->SetText(__strOrgAuthor);
       __pEFKeyword->SetText(__strOrgKeyword);
    }
  2. Updating Content Info

    When the content of the EditField instances has been altered, the UpdateContent() method updates the new content info into the content database and displays the results of this task in a pop-up.

    void DetailForm::UpdateContent(void)
    {
       bool isModified = false;
       result r = E_SUCCESS;
       String popStr;
       ContentManager contentMgr;
    
       __pCommonPopup->SetMessage("Updating...");
       __pCommonPopup->Show();
    
       // Check an Author field
       if (__strOrgAuthor.Equals(__pEFAuthor->GetText(), true) == false)
       {
          isModified = true;
          r = __pContentInfo->SetAuthor(__pEFAuthor->GetText());
       }
    
       // Check a Keyword field
       if (__strOrgKeyword.Equals(__pEFKeyword->GetText(), true) == false)
       {
          isModified = true;
          r = __pContentInfo->SetKeyword(__pEFKeyword->GetText());
       }
    
       // Update
       if (isModified == true)
       {
          r = contentMgr.Construct();
          r = contentMgr.UpdateContent(*__pContentInfo);
    
          popStr = "Update is completed.";
    
          __strOrgAuthor = __pEFAuthor->GetText();
          __strOrgKeyword = __pEFKeyword->GetText();
       }
       else
       {
          popStr = "Update failed.";
       }
    }
  3. Deleting Content

    The DeleteContent() method simply deletes the file that is currently being examined.

    void DetailForm::DeleteContent(void)
    {
       String popStr;
       result r = E_SUCCESS;
    
       ContentManager contentMgr;
    
       __pCommonPopup->SetMessage("Deleting...");
       __pCommonPopup->Show();
    
       r = contentMgr.Construct();
    
       r = contentMgr.DeleteContent(__pContentInfo->GetContentId());
    
       __pContentInfo = null;
       popStr = "Delete completed.";
    }

Where to Go Next