// Methods used by instantiator/creator and/or self
OP_BOOLEAN TransferManagerDownloadCallback::Execute()
{
	OP_STATUS status;
	BOOL done = FALSE;

	if (!cancelled)
	{
		if (called)
		{
			switch (download_action_mode)
			{
			case DOWNLOAD_UNDECIDED:
				// TODO: This should not be possible, need to test......
				// Use this to throw things back at document manager
				done = TRUE;
				break;
			case DOWNLOAD_ABORT:
				if (keep_loading)
				{
					if (document_manager)
						document_manager->SetLoadStatus(WAIT_FOR_LOADING_FINISHED);
				}
				else
				{
					if (document_manager)
						document_manager->StopLoading(FALSE);
					if (!download_url.GetRep()->GetUsedCount()) // These two lines belong in the url module
						download_url.Unload();					// rfz 20071028
				}
				EnableWindowClose();
				done = TRUE;
				break;
			case DOWNLOAD_SAVE:
			case DOWNLOAD_RUN:
				if (!dont_add_to_transfers)
				{
					BOOL show_transfer = TRUE;
					if (download_context)
						show_transfer = download_context->IsShownDownload();

					TransferItem* newitem = 0;
					// Put the download object into TransferManager
					status = ((TransferManager*)g_transferManager)->AddTransferItem(download_url,
							download_filename.CStr(),
							OpTransferItem::ACTION_UNKNOWN,
							FALSE,
							0,
							TransferItem::TRANSFERTYPE_DOWNLOAD,
							NULL,
							NULL,
							FALSE,
							show_transfer,
							m_privacy_mode,
							&newitem);
					if (status == OpStatus::OK)
					{
						if (download_context && download_context->GetTransferListener())
							newitem->SetTransferListener(download_context->GetTransferListener());

						if (download_action_mode == DOWNLOAD_SAVE)
							newitem->SetAction(OpTransferItem::ACTION_SAVE);
						else if (download_action_mode == DOWNLOAD_RUN)
							newitem->SetAction(OpTransferItem::ACTION_RUN_WHEN_FINISHED);

						newitem->SetViewer(viewer);

						if (need_copy_when_downloaded)
							newitem->SetCopyWhenDownloaded(download_filename);

						MessageHandler *oldHandler = download_url.GetFirstMessageHandler();

						// Check if this URL is already being downloaded to some file
						// (the same file or another file - who knows?). The Opera core
						// doesn't handle concurrent downloading of the same URL properly.
						// So just give up.
						//
						// The result will be that the transfer already in progress completes
						// without problems, while the new transfer that was attempted added,
						// will hang (the progress bar in the window that triggered the transfer
						// will stay there until the user aborts it somehow).
						MessageHandler *currentHandler = oldHandler;
						BOOL transfer_in_progress = FALSE;
						while (currentHandler && !transfer_in_progress)
						{
							if (currentHandler == g_main_message_handler)
								transfer_in_progress = TRUE;
							else
								currentHandler = static_cast<MessageHandler *>(currentHandler->Suc());
						}

						if (!transfer_in_progress)
						{
							FramesDocument *doc = document_manager ? document_manager->GetCurrentDoc() : NULL;

							URLStatus ust = download_url.Status(FALSE);
							switch (ust)
							{
							case URL_UNLOADED:
								download_url.Load(g_main_message_handler, doc ? doc->GetURL() : URL());
								currentHandler = g_main_message_handler;
								break;

							case URL_LOADING_ABORTED:
								download_url.ResumeLoad(g_main_message_handler, doc ? doc->GetURL() : URL());
								currentHandler = g_main_message_handler;
								break;

							default:

								if ((ust == URL_LOADED || ust == URL_LOADING_FAILURE) && is_download_to && !download_to_started)
								{
									// Fix for DSK-232055 - the best we can do is to reload
									URL ref = download_url.GetAttribute(URL::KReferrerURL);
									download_url.SetAttribute(URL::KReloadSameTarget, TRUE);
									download_url.Reload(g_main_message_handler, ref, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE);
									currentHandler = g_main_message_handler;
								}
								else
								{
									if (oldHandler && !keep_loading)
									{
										download_url.ChangeMessageHandler(oldHandler, g_main_message_handler);
										currentHandler = g_main_message_handler;
									}
									else
										currentHandler = oldHandler;	
								}
							}
						}
						// currentHandler is NULL when download_url.Status(FALSE) equals URL_LOADING_FAILED, or shall we set it to g_main_message_handler anyway?
						if (currentHandler)
							newitem->SetCallbacks(currentHandler, download_url.Id(TRUE));
					}
				}
				if (download_action_mode == DOWNLOAD_RUN)
				{
					if (	document_manager &&
							download_url.Status(FALSE) != URL_UNLOADED &&
							download_url.Status(TRUE) != URL_LOADING)
					{
						// The loading has already finished. We have to trigger the action
						// manually by using HandleAllLoaded() [espen]

						// InitiateTransfer() above destroys filename
						//				doc_man->SetCurrentURL(saved_url);

						if( viewer.GetAction() == VIEWER_APPLICATION || viewer.GetAction() == VIEWER_PASS_URL)
						{
							document_manager->SetAction(VIEWER_APPLICATION);
							document_manager->SetApplication(viewer.GetApplicationToOpenWith());
						}

						else if(viewer.GetAction() == VIEWER_REG_APPLICATION)
						{
							document_manager->SetAction(VIEWER_REG_APPLICATION);
						}
						// Restore
						//				doc_man->SetCurrentURL(GetUrl());
					}
				}
				if (dont_add_to_transfers)
				{
					if (download_context && download_context->GetTransferListener())
						download_context->GetTransferListener()->OnSavedFromCacheDone(download_filename);
				}
				done = TRUE;
				break;
			}

			// Here we clean up in the context of document manager
			if (document_manager && !keep_loading)
				document_manager->StopLoading(FALSE);	//the window is not handling the loading anymore

			if (document_manager && download_url.Status(TRUE) != URL_LOADING)
				document_manager->HandleAllLoaded(download_url.Id(TRUE));

			EnableWindowClose();
		}
		else
		{
#ifdef WEB_TURBO_MODE
			// Downloads should not use Turbo. NB! This WILL generate a second request for the same resource (this time without using the Turbo proxy)
			if (download_url.GetAttribute(URL::KUsesTurbo) &&
				!download_url.GetAttribute(URL::KTurboBypassed) &&
#ifdef _BITTORRENT_SUPPORT_
				download_url.ContentType() != URL_P2P_BITTORRENT &&
#endif // _BITTORRENT_SUPPORT_
				document_manager)
			{
				URLStatus ustat = (URLStatus)download_url.GetAttribute(URL::KLoadStatus, URL::KFollowRedirect);
				if (ustat == URL_LOADING)
					document_manager->StopLoading(FALSE);

				const OpStringC8 url_str = download_url.GetAttribute(URL::KName_With_Fragment_Username_Password_NOT_FOR_UI, URL::KNoRedirect);
				download_url = g_url_api->GetURL(url_str.CStr());
				document_manager->SetCurrentURL(download_url, TRUE);
			}
#endif // WEB_TURBO_MODE
			delayed = TRUE;
		}
	}
	if (done)
	{
		Window* win = document_manager ? document_manager->GetWindow():NULL;
		TryDelete();

		//if the window is Not intiated by the user and this is the first url the window loads, close it
		//since the data is handled elsewhere.
		if (win && win->GetOpenerWindow() && win->GetHistoryLen() == 0 && win->CanClose())
			win->Close();

		return OpBoolean::IS_TRUE; // Signifying that we have done a transition through a download initiation
	}
	else
	{
		return OpBoolean::IS_FALSE; // Signifying that we will back here before we are done
	}
}