Example #1
0
void GroupsModel::Commit()
{
	if (m_read_only)
		return;

	Account* account = MessageEngine::GetInstance()->GetAccountById(m_account_id);

	BOOL account_changed = FALSE;

	if (account)
	{
		account->StopFolderLoading();

		UINT32 index_to_show = 0;

		for (int pos = 0; pos < GetItemCount(); pos++)
		{
			GroupsModelItem* item = GetItemByIndex(pos);
			if (item)
			{
				OpString path, name;
				OpStatus::Ignore(item->GetPath(path));
				OpStatus::Ignore(item->GetName(name));

				if (item->PathIsChanged() && !item->IsManuallyAdded())
				{
					OpString old_path;
					OpStatus::Ignore(item->GetOldPath(old_path));
					OpStatus::Ignore(account->RenameFolder(old_path, path));
				}

				if (item->SubscriptionIsChanged())
				{
					if (item->IsManuallyAdded())
					{
						OpStatus::Ignore(account->CreateFolder(path, item->GetIsSubscribed()));
					}

					if (item->GetIsSubscribed())
					{
						OpStatus::Ignore(account->AddSubscribedFolder(path));
                        account->SetIsTemporary(FALSE);
						if (m_folder_type != OpTypedObject::IMAPFOLDER_TYPE)
						{
							Index* index = MessageEngine::GetInstance()->GetIndexer()->GetSubscribedFolderIndex(account, path, 0, name, TRUE, FALSE);
							if (index)
							{
								MessageEngine::GetInstance()->RefreshFolder(index->GetId());
								if (index_to_show)
									index_to_show = IndexTypes::FIRST_ACCOUNT + m_account_id;
								else
									index_to_show = index->GetId();
							}
						}
					}
					else
					{
						Index* index = MessageEngine::GetInstance()->GetIndexer()->GetSubscribedFolderIndex(account, path, 0, name, FALSE, FALSE);

						if (index)
							OpStatus::Ignore(account->RemoveSubscribedFolder(index->GetId()));
						else
							OpStatus::Ignore(account->RemoveSubscribedFolder(path));
					}
					account_changed = TRUE;
				}
				else if (item->GetIsSubscribed() && m_folder_type != OpTypedObject::IMAPFOLDER_TYPE)
				{
					MessageEngine::GetInstance()->GetIndexer()->GetSubscribedFolderIndex(account, path, 0, name, TRUE, FALSE);
				}
			}
		}
		if (account_changed)
		{
			account->CommitSubscribedFolders();
		}

		if (index_to_show)
		{
			g_application->GoToMailView(index_to_show, NULL, NULL, TRUE, FALSE, TRUE);
		}
	}
}