270
edits
Line 137: | Line 137: | ||
gActivityManager.registerActivityViewType("junk_filtering", VIEW_TYPE_STANDALONE); | gActivityManager.registerActivityViewType("junk_filtering", VIEW_TYPE_STANDALONE); | ||
=== Showing an user-defined activity with cancel capability === | === Showing an user-defined activity with cancel capability (JS)=== | ||
This sample will improve the previous one by providing an nsIActivityCancelable to allow the user cancel the process. | This sample will improve the previous one by providing an nsIActivityCancelable to allow the user cancel the process. | ||
Line 176: | Line 176: | ||
Since nsIActivityCancelable is provided with the activity, UI will show a cancel button besides the activity. You can extrapolate this sample to nsIActivityRetryable and nsIActivityPausable as well. | Since nsIActivityCancelable is provided with the activity, UI will show a cancel button besides the activity. You can extrapolate this sample to nsIActivityRetryable and nsIActivityPausable as well. | ||
=== Showing an user-defined activity with undo capability (C++) === | |||
..... | |||
#include "nsIActivity.h" | |||
#include "nsIActivityManager.h" | |||
..... | |||
////////////////////////////////////////////////////////////////////////////// | |||
//// Undo handler implementation | |||
class myCopyEventUndo : public nsIActivityUndoHandler | |||
{ | |||
public: | |||
NS_DECL_ISUPPORTS | |||
NS_DECL_NSIACTIVITYUNDOHANDLER | |||
myCopyEventUndo() {} | |||
private: | |||
~myCopyEventUndo() {} | |||
}; | |||
NS_IMPL_ISUPPORTS1(myCopyEventUndo, nsIActivityUndoHandler) | |||
NS_IMETHODIMP myCopyEventUndo::Undo(nsIActivityEvent *event, nsresult *result) | |||
{ | |||
nsresult rv; | |||
// get the subjects of this copy event | |||
PRUint32 length; | |||
nsIVariant **subjectList; | |||
rv = event->GetSubjects(&length, &subjectList); | |||
if(NS_FAILED(rv)) | |||
return rv; | |||
// first subject in the list is the source folder in this particular case | |||
nsCOMPtr<nsIMsgFolder> folder = do_QueryInterface(subjectList[0]); | |||
// get the initiator | |||
nsIVariant *initiator; | |||
event->GetInitiator(&initiator); | |||
if (initiator) | |||
{ | |||
nsISupports* ptr; | |||
rv = object->GetAsISupports(&ptr); | |||
if(NS_FAILED(rv)) | |||
return rv; | |||
nsCOMPtr<nsIMsgCopyService> copyService = do_QueryInterface(ptr); | |||
if (copyService) | |||
copyService->Undo(folder); | |||
} | |||
*result = NS_OK; | |||
return NS_OK; | |||
} | |||
///////////////////////////////////////////////////////////////////////////// | |||
//// Creating an undoable copy event | |||
nsCOMPtr<nsIActivityUndoHandler> undoHandler = new myCopyEventUndo(); | |||
nsCOMPtr<nsIActivityEvent> copyEvent(do_CreateInstance("@mozilla.org/activity-event;1")); | |||
// The initiator of this particular event is CopyService. We want to | |||
// associate it with the event since we are going to use it in the | |||
// undo handler. | |||
// Same for the source folder, it is required for undo operation, but | |||
// since it is the subject of this activity (event), it goes into the | |||
// subject list. | |||
// wrap copyservice in a nsvariant component | |||
nsCOMPtr<nsIWritableVariant> initiator = do_CreateInstance(NS_VARIANT_CONTRACTID); | |||
initiator->SetAsISupports(reinterpret_cast<nsISupports*>(copyService)); | |||
// subject of the delete operation is the imap folder | |||
// wrap it in a nsvariant component | |||
nsCOMPtr<nsIWritableVariant> srcFolder = do_CreateInstance(NS_VARIANT_CONTRACTID); | |||
srcFolder->SetAsISupports(reinterpret_cast<nsISupports*>(imapFolder)); | |||
copyEvent->AddSubject(srcFolder); | |||
copyEvent->InitWithHandlers( | |||
NS_LITERAL_STRING("Message copy event"), | |||
NS_LITERAL_STRING("Completed successfully"), | |||
PR_Now() / PR_USEC_PER_MSEC, // start time | |||
PR_Now() / PR_USEC_PER_MSEC, // completion time | |||
initiator, | |||
undoHandler); | |||
//////////////////////////////////////////////////////////////// | |||
//// Adding the event into Activity Manager | |||
nsCOMPtr<nsIActivityManager> activityMgr(do_GetService("@mozilla.org/activity-manager;1")); | |||
PRUint32 id; | |||
activityMgr->AddActivity(copyEvent, &id); | |||
=== Adding an activity with custom context type === | === Adding an activity with custom context type === |
edits