⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nameswapappui.cpp

📁 这一个应用于symbian 操作系统
💻 CPP
字号:
/*
    This file is part of NameSwap.

    NameSwap is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    NameSwap is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NameSwap; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  	Copyright (C) 2002 Ashley Montanaro.
*/

#include "NameSwapAppUi.h"
#include "NameSwapDialog.h" 
#include <NameSwap.rsg>
#include "nameswap.hrh"

#include <avkon.hrh>

void CNameSwapAppUi::ConstructL()
    {
    BaseConstructL();
    iAppDialog = new (ELeave) CNameSwapDialog;
	iAppDialog->SetMopParent(this);
    iAppDialog->ExecuteLD( R_NAMESWAP_DIALOG );
    AddToStackL( iAppDialog );

	iNameSwapAO = new (ELeave) CNameSwapAO;
    }

CNameSwapAppUi::~CNameSwapAppUi()
    {
    if (iAppDialog)
        {
        RemoveFromStack( iAppDialog );
        delete iAppDialog;
        }

	delete iNameSwapAO;
	}

void CNameSwapAppUi::DynInitMenuPaneL(TInt /*aResourceId*/,
									  CEikMenuPane* /*aMenuPane*/)
    {
    }

TKeyResponse CNameSwapAppUi::HandleKeyEventL(const TKeyEvent& /*aKeyEvent*/,
											 TEventCode /*aType*/)
    {
	// We're not interested in any of the keys.
    return EKeyWasNotConsumed;
    }

void CNameSwapAppUi::HandleCommandL(TInt aCommand)
    {
    switch (aCommand)
        {
		case EEikCmdExit:
            Exit();
            break;

        case ENameSwapCmdAppSwapIndividually:
			SwapEachContactL();
            break;

		case ENameSwapCmdAppSwapAll:
			SwapAllContactsL();
            break;

        default:
            break;      
        }
    }

void CNameSwapAppUi::SwapEachContactL()
	{
	// Open the contacts database.
	CContactDatabase* database;
	database = CContactDatabase::OpenL();
	CleanupStack::PushL(database);

	// Get all items in the DB into a list.
	const CContactIdArray* contacts = database->SortedItemsL();

	CContactItem* contact;
	HBufC* firstNameBuf;
	HBufC* lastNameBuf;

	// Lines for query window.
	TBuf<256> line1;
	TBuf<256> line2;

	for (TInt i = 0; i < contacts->Count(); i++)
		{
		// Open each contact in turn, pushing it onto the cleanup stack.
		contact = database->OpenContactLX((*contacts)[i]);

		CContactItemFieldSet& fieldSet = contact->CardFields();

		// Get first name.

		TInt index = fieldSet.Find(KUidContactFieldGivenName);

		// Check that the first name field is actually there.
		if ((index < 0) || (index >= fieldSet.Count()))
			{
			CleanupStack::PopAndDestroy(); // contact
			continue;
			}

		CContactItemField& firstNameField = fieldSet[index];
		CContactTextField* firstName = firstNameField.TextStorage();

		firstNameBuf = firstName->Text().AllocLC();

		// Get last name.

		index = fieldSet.Find(KUidContactFieldFamilyName);

		// Check that the last name field is actually there.
		if ((index < 0) || (index >= fieldSet.Count()))
			{
			CleanupStack::PopAndDestroy(2); // contact, firstNameBuf
			continue;
			}

		CContactItemField& lastNameField = fieldSet[index];
		CContactTextField* lastName = lastNameField.TextStorage();

		lastNameBuf = lastName->Text().AllocLC();

		// Set up strings for query window.
		line1.Format(_L("\"%S %S\"\n =>"), lastNameBuf, firstNameBuf);
		line2.Format(_L("\"%S %S\" ?"), firstNameBuf, lastNameBuf);

		// If the contact has both first and last names defined, and the user
		// says "Yes", swap the contact's names.
		if ((firstNameBuf->Length() > 0) && ((*firstNameBuf) != KSpace) &&
			(lastNameBuf->Length() > 0) && ((*lastNameBuf) != KSpace) &&
			iEikonEnv->QueryWinL(line1, line2))
			{
			// Note that the contacts DB takes ownership of the HBufC's we pass in,
			// so we don't need to delete them.
			firstName->SetText(lastNameBuf);
			lastName->SetText(firstNameBuf);

			// Action! Note that this command closes the contact as well, so we don't
			// need to do so.
			database->CommitContactL(*contact);

			CleanupStack::Pop(3); // contact, firstNameBuf, lastNameBuf
			}
		else
			{
			CleanupStack::PopAndDestroy(3); // contact, firstNameBuf, lastNameBuf
			}
		
		// Synchronously compress the database.
		database->CompactL();
		}

	CleanupStack::PopAndDestroy(); // database
	}


void CNameSwapAppUi::SwapAllContactsL()
	{
	// Check that the user's sure.
	if (!iEikonEnv->QueryWinL(_L("Swap names of"), _L("all contacts in database?")))
		return;

	// Start the asynchronous contact-swapping process.
	iNameSwapAO->StartL();
	}




// End of File  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -