📄 nameswapao.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 <NameSwap.rsg>
#include "NameSwap.hrh"
#include "NameSwapAO.h"
// This active object is responsible for swapping names when "swap all" is selected
// by the user. It's required because progress dialogs use an active object which
// needs to be scheduled. All the operations on the contacts database are synchronous,
// so this object just reschedules itself after swapping each contact's names.
CNameSwapAO::CNameSwapAO() : CActive(EPriorityIdle)
{
CActiveScheduler::Add(this);
}
CNameSwapAO::~CNameSwapAO()
{
delete iDatabase;
}
void CNameSwapAO::StartL()
{
// Open the database and get all the contacts into a list.
iDatabase = CContactDatabase::OpenL();
iContacts = iDatabase->SortedItemsL();
// Create the progress dialog, specifying that it should be displayed immediately.
// If this flag isn't passed, it doesn't seem to display at all (?)
iProgressDialog = new (ELeave) CAknProgressDialog(
(REINTERPRET_CAST(CEikDialog**,&iProgressDialog)), ETrue);
iProgressDialog->PrepareLC(R_PROGRESS_NOTE);
// Get a handle to the progress info. Must be done after PrepareLC().
iProgressInfo = iProgressDialog->GetProgressInfoL();
// Tell the dialog to notify us if the user presses "Cancel".
iProgressDialog->SetCallback(this);
// Action!
iProgressDialog->RunLD();
iContactIndex = 0;
// If there are actually any contacts in the DB, start by swapping the first one.
if (iContacts->Count() > 0)
{
SwapContactL(iContactIndex);
iProgressInfo->SetFinalValue(iContacts->Count() - 1);
}
// Reschedule ourselves.
SetActive();
TRequestStatus* status = &iStatus;
User::RequestComplete(status, KErrNone);
}
void CNameSwapAO::RunL()
{
// If there's been an error, bail out straight away.
User::LeaveIfError(iStatus.Int());
// Update the progress information.
iProgressInfo->IncrementAndDraw(1);
iContactIndex++;
if (iContactIndex < iContacts->Count())
{
// Swap the next contact.
SwapContactL(iContactIndex);
// Synchronously compress the database.
iDatabase->CompactL();
// Reschedule ourselves.
SetActive();
TRequestStatus* status = &iStatus;
User::RequestComplete(status, KErrNone);
}
else
{
// Get rid of the progress dialog (deletes it as well).
iProgressDialog->ProcessFinishedL();
// Close the database.
delete iDatabase;
iDatabase = NULL;
}
}
void CNameSwapAO::DoCancel()
{
// Synchronously compress the database.
iDatabase->CompactL();
delete iDatabase;
iDatabase = NULL;
}
TInt CNameSwapAO::RunError(TInt /*aError*/)
{
// Synchronously compress the database.
iDatabase->CompactL();
delete iDatabase;
iDatabase = NULL;
// Just return KErrNone to stop the active scheduler panicking.
return KErrNone;
}
void CNameSwapAO::DialogDismissedL(TInt /*aButtonId*/)
{
// Cancel anything that's still going on.
Cancel();
}
void CNameSwapAO::SwapContactL(TInt aIndex)
{
CContactItem* contact;
HBufC* firstNameBuf;
HBufC* lastNameBuf;
// Open the contact, pushing it onto the cleanup stack.
contact = iDatabase->OpenContactLX((*iContacts)[aIndex]);
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
return;
}
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
return;
}
CContactItemField& lastNameField = fieldSet[index];
CContactTextField* lastName = lastNameField.TextStorage();
lastNameBuf = lastName->Text().AllocLC();
// If the contact has both first and last names defined, swap the names.
if ((firstNameBuf->Length() > 0) && ((*firstNameBuf) != KSpace) &&
(lastNameBuf->Length() > 0) && ((*lastNameBuf) != KSpace))
{
// 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.
iDatabase->CommitContactL(*contact);
CleanupStack::Pop(3); // contact, firstNameBuf, lastNameBuf
}
else
{
CleanupStack::PopAndDestroy(3); // contact, firstNameBuf, lastNameBuf
}
}
// End of file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -