editor/composer/src/nsComposerDocumentCommands.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/editor/composer/src/nsComposerDocumentCommands.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,497 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +#include "nsAutoPtr.h"                  // for nsRefPtr, getter_AddRefs, etc
    1.11 +#include "nsCOMPtr.h"                   // for nsCOMPtr, do_QueryInterface, etc
    1.12 +#include "nsCRT.h"                      // for nsCRT
    1.13 +#include "nsComposerCommands.h"         // for nsSetDocumentOptionsCommand, etc
    1.14 +#include "nsDebug.h"                    // for NS_ENSURE_ARG_POINTER, etc
    1.15 +#include "nsError.h"                    // for NS_ERROR_INVALID_ARG, etc
    1.16 +#include "nsICommandParams.h"           // for nsICommandParams
    1.17 +#include "nsIDOMDocument.h"             // for nsIDOMDocument
    1.18 +#include "nsIDocShell.h"                // for nsIDocShell
    1.19 +#include "nsIDocument.h"                // for nsIDocument
    1.20 +#include "nsIEditingSession.h"          // for nsIEditingSession, etc
    1.21 +#include "nsIEditor.h"                  // for nsIEditor
    1.22 +#include "nsIHTMLEditor.h"              // for nsIHTMLEditor
    1.23 +#include "nsIHTMLInlineTableEditor.h"   // for nsIHTMLInlineTableEditor
    1.24 +#include "nsIHTMLObjectResizer.h"       // for nsIHTMLObjectResizer
    1.25 +#include "nsIPlaintextEditor.h"         // for nsIPlaintextEditor, etc
    1.26 +#include "nsIPresShell.h"               // for nsIPresShell
    1.27 +#include "nsISelectionController.h"     // for nsISelectionController
    1.28 +#include "nsISupportsImpl.h"            // for nsPresContext::Release
    1.29 +#include "nsISupportsUtils.h"           // for NS_IF_ADDREF
    1.30 +#include "nsIURI.h"                     // for nsIURI
    1.31 +#include "nsPresContext.h"              // for nsPresContext
    1.32 +#include "nscore.h"                     // for NS_IMETHODIMP, nsresult, etc
    1.33 +
    1.34 +class nsISupports;
    1.35 +
    1.36 +//defines
    1.37 +#define STATE_ENABLED  "state_enabled"
    1.38 +#define STATE_ALL "state_all"
    1.39 +#define STATE_ATTRIBUTE "state_attribute"
    1.40 +#define STATE_DATA "state_data"
    1.41 +
    1.42 +static
    1.43 +nsresult
    1.44 +GetPresContextFromEditor(nsIEditor *aEditor, nsPresContext **aResult)
    1.45 +{
    1.46 +  NS_ENSURE_ARG_POINTER(aResult);
    1.47 +  *aResult = nullptr;
    1.48 +  NS_ENSURE_ARG_POINTER(aEditor);
    1.49 +
    1.50 +  nsCOMPtr<nsISelectionController> selCon;
    1.51 +  nsresult rv = aEditor->GetSelectionController(getter_AddRefs(selCon));
    1.52 +  NS_ENSURE_SUCCESS(rv, rv);
    1.53 +  NS_ENSURE_TRUE(selCon, NS_ERROR_FAILURE);
    1.54 +
    1.55 +  nsCOMPtr<nsIPresShell> presShell = do_QueryInterface(selCon);
    1.56 +  NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE);
    1.57 +
    1.58 +  NS_IF_ADDREF(*aResult = presShell->GetPresContext());
    1.59 +  return NS_OK;
    1.60 +}
    1.61 +
    1.62 +NS_IMETHODIMP
    1.63 +nsSetDocumentOptionsCommand::IsCommandEnabled(const char * aCommandName,
    1.64 +                                              nsISupports *refCon,
    1.65 +                                              bool *outCmdEnabled)
    1.66 +{
    1.67 +  NS_ENSURE_ARG_POINTER(outCmdEnabled);
    1.68 +  nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
    1.69 +  if (editor)
    1.70 +    return editor->GetIsSelectionEditable(outCmdEnabled);
    1.71 +
    1.72 +  *outCmdEnabled = false;
    1.73 +  return NS_OK;
    1.74 +}
    1.75 +
    1.76 +NS_IMETHODIMP
    1.77 +nsSetDocumentOptionsCommand::DoCommand(const char *aCommandName,
    1.78 +                                       nsISupports *refCon)
    1.79 +{
    1.80 +  return NS_ERROR_NOT_IMPLEMENTED;
    1.81 +}
    1.82 +
    1.83 +NS_IMETHODIMP
    1.84 +nsSetDocumentOptionsCommand::DoCommandParams(const char *aCommandName,
    1.85 +                                             nsICommandParams *aParams,
    1.86 +                                             nsISupports *refCon)
    1.87 +{
    1.88 +  NS_ENSURE_ARG_POINTER(aParams);
    1.89 +
    1.90 +  nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
    1.91 +  NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG);
    1.92 +
    1.93 +  nsRefPtr<nsPresContext> presContext;
    1.94 +  nsresult rv = GetPresContextFromEditor(editor, getter_AddRefs(presContext));
    1.95 +  NS_ENSURE_SUCCESS(rv, rv);
    1.96 +  NS_ENSURE_TRUE(presContext, NS_ERROR_FAILURE);
    1.97 +
    1.98 +  int32_t animationMode; 
    1.99 +  rv = aParams->GetLongValue("imageAnimation", &animationMode);
   1.100 +  if (NS_SUCCEEDED(rv))
   1.101 +  {
   1.102 +    // for possible values of animation mode, see:
   1.103 +    // http://lxr.mozilla.org/seamonkey/source/image/public/imgIContainer.idl
   1.104 +    presContext->SetImageAnimationMode(animationMode);
   1.105 +  }
   1.106 +
   1.107 +  bool allowPlugins; 
   1.108 +  rv = aParams->GetBooleanValue("plugins", &allowPlugins);
   1.109 +  if (NS_SUCCEEDED(rv))
   1.110 +  {
   1.111 +    nsCOMPtr<nsIDocShell> docShell(presContext->GetDocShell());
   1.112 +    NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
   1.113 +
   1.114 +    rv = docShell->SetAllowPlugins(allowPlugins);
   1.115 +    NS_ENSURE_SUCCESS(rv, rv);
   1.116 +  }
   1.117 +
   1.118 +  return NS_OK;
   1.119 +}
   1.120 +
   1.121 +NS_IMETHODIMP
   1.122 +nsSetDocumentOptionsCommand::GetCommandStateParams(const char *aCommandName,
   1.123 +                                                   nsICommandParams *aParams,
   1.124 +                                                   nsISupports *refCon)
   1.125 +{
   1.126 +  NS_ENSURE_ARG_POINTER(aParams);
   1.127 +  NS_ENSURE_ARG_POINTER(refCon);
   1.128 +
   1.129 +  // The base editor owns most state info
   1.130 +  nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
   1.131 +  NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG);
   1.132 +
   1.133 +  // Always get the enabled state
   1.134 +  bool outCmdEnabled = false;
   1.135 +  IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
   1.136 +  nsresult rv = aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled);
   1.137 +  NS_ENSURE_SUCCESS(rv, rv);
   1.138 +
   1.139 +  // get pres context
   1.140 +  nsRefPtr<nsPresContext> presContext;
   1.141 +  rv = GetPresContextFromEditor(editor, getter_AddRefs(presContext));
   1.142 +  NS_ENSURE_SUCCESS(rv, rv);
   1.143 +  NS_ENSURE_TRUE(presContext, NS_ERROR_FAILURE);
   1.144 +
   1.145 +  int32_t animationMode;
   1.146 +  rv = aParams->GetLongValue("imageAnimation", &animationMode);
   1.147 +  if (NS_SUCCEEDED(rv))
   1.148 +  {
   1.149 +    // for possible values of animation mode, see
   1.150 +    // http://lxr.mozilla.org/seamonkey/source/image/public/imgIContainer.idl
   1.151 +    rv = aParams->SetLongValue("imageAnimation",
   1.152 +                               presContext->ImageAnimationMode());
   1.153 +    NS_ENSURE_SUCCESS(rv, rv);
   1.154 +  }
   1.155 +
   1.156 +  bool allowPlugins = false; 
   1.157 +  rv = aParams->GetBooleanValue("plugins", &allowPlugins);
   1.158 +  if (NS_SUCCEEDED(rv))
   1.159 +  {
   1.160 +    nsCOMPtr<nsIDocShell> docShell(presContext->GetDocShell());
   1.161 +    NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
   1.162 +
   1.163 +    allowPlugins = docShell->PluginsAllowedInCurrentDoc();
   1.164 +
   1.165 +    rv = aParams->SetBooleanValue("plugins", allowPlugins);
   1.166 +    NS_ENSURE_SUCCESS(rv, rv);
   1.167 +  }
   1.168 +
   1.169 +  return NS_OK;
   1.170 +}
   1.171 +
   1.172 +
   1.173 +/**
   1.174 + *  Commands for document state that may be changed via doCommandParams
   1.175 + *  As of 11/11/02, this is just "cmd_setDocumentModified"
   1.176 + *  Note that you can use the same command class, nsSetDocumentStateCommand,
   1.177 + *    for more than one of this type of command
   1.178 + *    We check the input command param for different behavior
   1.179 + */
   1.180 +
   1.181 +NS_IMETHODIMP
   1.182 +nsSetDocumentStateCommand::IsCommandEnabled(const char * aCommandName,
   1.183 +                                            nsISupports *refCon,
   1.184 +                                            bool *outCmdEnabled)
   1.185 +{
   1.186 +  // These commands are always enabled
   1.187 +  NS_ENSURE_ARG_POINTER(outCmdEnabled);
   1.188 +  *outCmdEnabled = true;
   1.189 +  return NS_OK;
   1.190 +}
   1.191 +
   1.192 +NS_IMETHODIMP
   1.193 +nsSetDocumentStateCommand::DoCommand(const char *aCommandName,
   1.194 +                                     nsISupports *refCon)
   1.195 +{
   1.196 +  return NS_ERROR_NOT_IMPLEMENTED;
   1.197 +}
   1.198 +
   1.199 +NS_IMETHODIMP
   1.200 +nsSetDocumentStateCommand::DoCommandParams(const char *aCommandName,
   1.201 +                                           nsICommandParams *aParams,
   1.202 +                                           nsISupports *refCon)
   1.203 +{
   1.204 +  nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
   1.205 +  NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG);
   1.206 +
   1.207 +  if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentModified"))
   1.208 +  {
   1.209 +    NS_ENSURE_ARG_POINTER(aParams);
   1.210 +
   1.211 +    bool modified; 
   1.212 +    nsresult rv = aParams->GetBooleanValue(STATE_ATTRIBUTE, &modified);
   1.213 +
   1.214 +    // Should we fail if this param wasn't set?
   1.215 +    // I'm not sure we should be that strict
   1.216 +    NS_ENSURE_SUCCESS(rv, rv);
   1.217 +
   1.218 +    if (modified)
   1.219 +      return editor->IncrementModificationCount(1);
   1.220 +
   1.221 +    return editor->ResetModificationCount();
   1.222 +  }
   1.223 +
   1.224 +  if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentReadOnly"))
   1.225 +  {
   1.226 +    NS_ENSURE_ARG_POINTER(aParams);
   1.227 +    bool isReadOnly; 
   1.228 +    nsresult rvRO = aParams->GetBooleanValue(STATE_ATTRIBUTE, &isReadOnly);
   1.229 +    NS_ENSURE_SUCCESS(rvRO, rvRO);
   1.230 +
   1.231 +    uint32_t flags;
   1.232 +    editor->GetFlags(&flags);
   1.233 +    if (isReadOnly)
   1.234 +      flags |= nsIPlaintextEditor::eEditorReadonlyMask;
   1.235 +    else
   1.236 +      flags &= ~(nsIPlaintextEditor::eEditorReadonlyMask);
   1.237 +
   1.238 +    return editor->SetFlags(flags);
   1.239 +  }
   1.240 +
   1.241 +  if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentUseCSS"))
   1.242 +  {
   1.243 +    NS_ENSURE_ARG_POINTER(aParams);
   1.244 +    nsCOMPtr<nsIHTMLEditor> htmleditor = do_QueryInterface(refCon);
   1.245 +    NS_ENSURE_TRUE(htmleditor, NS_ERROR_INVALID_ARG);
   1.246 +
   1.247 +    bool desireCSS;
   1.248 +    nsresult rvCSS = aParams->GetBooleanValue(STATE_ATTRIBUTE, &desireCSS);
   1.249 +    NS_ENSURE_SUCCESS(rvCSS, rvCSS);
   1.250 +
   1.251 +    return htmleditor->SetIsCSSEnabled(desireCSS);
   1.252 +  }
   1.253 +
   1.254 +  if (!nsCRT::strcmp(aCommandName, "cmd_insertBrOnReturn"))
   1.255 +  {
   1.256 +    NS_ENSURE_ARG_POINTER(aParams);
   1.257 +    nsCOMPtr<nsIHTMLEditor> htmleditor = do_QueryInterface(refCon);
   1.258 +    NS_ENSURE_TRUE(htmleditor, NS_ERROR_INVALID_ARG);
   1.259 +
   1.260 +    bool insertBrOnReturn;
   1.261 +    nsresult rvBR = aParams->GetBooleanValue(STATE_ATTRIBUTE,
   1.262 +                                              &insertBrOnReturn);
   1.263 +    NS_ENSURE_SUCCESS(rvBR, rvBR);
   1.264 +
   1.265 +    return htmleditor->SetReturnInParagraphCreatesNewParagraph(!insertBrOnReturn);
   1.266 +  }
   1.267 +
   1.268 +  if (!nsCRT::strcmp(aCommandName, "cmd_enableObjectResizing"))
   1.269 +  {
   1.270 +    NS_ENSURE_ARG_POINTER(aParams);
   1.271 +    nsCOMPtr<nsIHTMLObjectResizer> resizer = do_QueryInterface(refCon);
   1.272 +    NS_ENSURE_TRUE(resizer, NS_ERROR_INVALID_ARG);
   1.273 +
   1.274 +    bool enabled;
   1.275 +    nsresult rvOR = aParams->GetBooleanValue(STATE_ATTRIBUTE, &enabled);
   1.276 +    NS_ENSURE_SUCCESS(rvOR, rvOR);
   1.277 +
   1.278 +    return resizer->SetObjectResizingEnabled(enabled);
   1.279 +  }
   1.280 +
   1.281 +  if (!nsCRT::strcmp(aCommandName, "cmd_enableInlineTableEditing"))
   1.282 +  {
   1.283 +    NS_ENSURE_ARG_POINTER(aParams);
   1.284 +    nsCOMPtr<nsIHTMLInlineTableEditor> editor = do_QueryInterface(refCon);
   1.285 +    NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG);
   1.286 +
   1.287 +    bool enabled;
   1.288 +    nsresult rvOR = aParams->GetBooleanValue(STATE_ATTRIBUTE, &enabled);
   1.289 +    NS_ENSURE_SUCCESS(rvOR, rvOR);
   1.290 +
   1.291 +    return editor->SetInlineTableEditingEnabled(enabled);
   1.292 +  }
   1.293 +
   1.294 +  return NS_ERROR_NOT_IMPLEMENTED;
   1.295 +}
   1.296 +
   1.297 +NS_IMETHODIMP
   1.298 +nsSetDocumentStateCommand::GetCommandStateParams(const char *aCommandName,
   1.299 +                                                 nsICommandParams *aParams,
   1.300 +                                                 nsISupports *refCon)
   1.301 +{
   1.302 +  NS_ENSURE_ARG_POINTER(aParams);
   1.303 +  NS_ENSURE_ARG_POINTER(refCon);
   1.304 +
   1.305 +  // The base editor owns most state info
   1.306 +  nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
   1.307 +  NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG);
   1.308 +
   1.309 +  // Always get the enabled state
   1.310 +  bool outCmdEnabled = false;
   1.311 +  IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
   1.312 +  nsresult rv = aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled);
   1.313 +  NS_ENSURE_SUCCESS(rv, rv);
   1.314 +
   1.315 +  if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentModified"))
   1.316 +  {
   1.317 +    bool modified;
   1.318 +    rv = editor->GetDocumentModified(&modified);
   1.319 +    NS_ENSURE_SUCCESS(rv, rv);
   1.320 +
   1.321 +    return aParams->SetBooleanValue(STATE_ATTRIBUTE, modified);
   1.322 +  }
   1.323 +
   1.324 +  if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentReadOnly"))
   1.325 +  {
   1.326 +    NS_ENSURE_ARG_POINTER(aParams);
   1.327 +
   1.328 +    uint32_t flags;
   1.329 +    editor->GetFlags(&flags);
   1.330 +    bool isReadOnly = flags & nsIPlaintextEditor::eEditorReadonlyMask;
   1.331 +    return aParams->SetBooleanValue(STATE_ATTRIBUTE, isReadOnly);
   1.332 +  }
   1.333 +
   1.334 +  if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentUseCSS"))
   1.335 +  {
   1.336 +    NS_ENSURE_ARG_POINTER(aParams);
   1.337 +    nsCOMPtr<nsIHTMLEditor> htmleditor = do_QueryInterface(refCon);
   1.338 +    NS_ENSURE_TRUE(htmleditor, NS_ERROR_INVALID_ARG);
   1.339 +
   1.340 +    bool isCSS;
   1.341 +    htmleditor->GetIsCSSEnabled(&isCSS);
   1.342 +    return aParams->SetBooleanValue(STATE_ALL, isCSS);
   1.343 +  }
   1.344 +
   1.345 +  if (!nsCRT::strcmp(aCommandName, "cmd_insertBrOnReturn"))
   1.346 +  {
   1.347 +    NS_ENSURE_ARG_POINTER(aParams);
   1.348 +    nsCOMPtr<nsIHTMLEditor> htmleditor = do_QueryInterface(refCon);
   1.349 +    NS_ENSURE_TRUE(htmleditor, NS_ERROR_INVALID_ARG);
   1.350 +
   1.351 +    bool createPOnReturn;
   1.352 +    htmleditor->GetReturnInParagraphCreatesNewParagraph(&createPOnReturn);
   1.353 +    return aParams->SetBooleanValue(STATE_ATTRIBUTE, !createPOnReturn);
   1.354 +  }
   1.355 +
   1.356 +  if (!nsCRT::strcmp(aCommandName, "cmd_enableObjectResizing"))
   1.357 +  {
   1.358 +    NS_ENSURE_ARG_POINTER(aParams);
   1.359 +    nsCOMPtr<nsIHTMLObjectResizer> resizer = do_QueryInterface(refCon);
   1.360 +    NS_ENSURE_TRUE(resizer, NS_ERROR_INVALID_ARG);
   1.361 +
   1.362 +    bool enabled;
   1.363 +    resizer->GetObjectResizingEnabled(&enabled);
   1.364 +    return aParams->SetBooleanValue(STATE_ATTRIBUTE, enabled);
   1.365 +  }
   1.366 +
   1.367 +  if (!nsCRT::strcmp(aCommandName, "cmd_enableInlineTableEditing"))
   1.368 +  {
   1.369 +    NS_ENSURE_ARG_POINTER(aParams);
   1.370 +    nsCOMPtr<nsIHTMLInlineTableEditor> editor = do_QueryInterface(refCon);
   1.371 +    NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG);
   1.372 +
   1.373 +    bool enabled;
   1.374 +    editor->GetInlineTableEditingEnabled(&enabled);
   1.375 +    return aParams->SetBooleanValue(STATE_ATTRIBUTE, enabled);
   1.376 +  }
   1.377 +
   1.378 +  return NS_ERROR_NOT_IMPLEMENTED;
   1.379 +}
   1.380 +
   1.381 +/**
   1.382 + * Commands just for state notification 
   1.383 + *  As of 11/21/02, possible commands are:
   1.384 + *    "obs_documentCreated"
   1.385 + *    "obs_documentWillBeDestroyed"
   1.386 + *    "obs_documentLocationChanged"
   1.387 + *  Note that you can use the same command class, nsDocumentStateCommand
   1.388 + *    for these or future observer commands.
   1.389 + *    We check the input command param for different behavior
   1.390 + *
   1.391 + *  How to use:
   1.392 + *  1. Get the nsICommandManager for the current editor
   1.393 + *  2. Implement an nsIObserve object, e.g:
   1.394 + *
   1.395 + *    void Observe( 
   1.396 + *        in nsISupports aSubject, // The nsICommandManager calling this Observer
   1.397 + *        in string      aTopic,   // command name, e.g.:"obs_documentCreated"
   1.398 + *                                 //    or "obs_documentWillBeDestroyed"
   1.399 +          in wstring     aData );  // ignored (set to "command_status_changed")
   1.400 + *
   1.401 + *  3. Add the observer by:
   1.402 + *       commandManager.addObserver(observeobject, obs_documentCreated);
   1.403 + *  4. In the appropriate location in editorSession, editor, or commands code, 
   1.404 + *     trigger the notification of this observer by something like:
   1.405 + *
   1.406 + *  nsCOMPtr<nsICommandManager> commandManager = do_GetInterface(mDocShell);
   1.407 + *  nsCOMPtr<nsPICommandUpdater> commandUpdater = do_QueryInterface(commandManager);
   1.408 + *  NS_ENSURE_TRUE(commandUpdater, NS_ERROR_FAILURE);
   1.409 + *    commandUpdater->CommandStatusChanged(obs_documentCreated);
   1.410 + *
   1.411 + *  5. Use GetCommandStateParams() to obtain state information
   1.412 + *     e.g., any creation state codes when creating an editor are 
   1.413 + *     supplied for "obs_documentCreated" command in the 
   1.414 + *     "state_data" param's value
   1.415 + *
   1.416 + */
   1.417 +
   1.418 +NS_IMETHODIMP
   1.419 +nsDocumentStateCommand::IsCommandEnabled(const char* aCommandName,
   1.420 +                                         nsISupports *refCon,
   1.421 +                                         bool *outCmdEnabled)
   1.422 +{
   1.423 +  NS_ENSURE_ARG_POINTER(outCmdEnabled);
   1.424 +  // Always return false to discourage callers from using DoCommand()
   1.425 +  *outCmdEnabled = false;
   1.426 +  return NS_OK;
   1.427 +}
   1.428 +
   1.429 +NS_IMETHODIMP
   1.430 +nsDocumentStateCommand::DoCommand(const char *aCommandName,
   1.431 +                                  nsISupports *refCon)
   1.432 +{
   1.433 +  return NS_ERROR_NOT_IMPLEMENTED;
   1.434 +}
   1.435 +
   1.436 +NS_IMETHODIMP
   1.437 +nsDocumentStateCommand::DoCommandParams(const char *aCommandName,
   1.438 +                                        nsICommandParams *aParams,
   1.439 +                                        nsISupports *refCon)
   1.440 +{
   1.441 +  return NS_ERROR_NOT_IMPLEMENTED;
   1.442 +}
   1.443 +
   1.444 +NS_IMETHODIMP
   1.445 +nsDocumentStateCommand::GetCommandStateParams(const char *aCommandName,
   1.446 +                                              nsICommandParams *aParams,
   1.447 +                                              nsISupports *refCon)
   1.448 +{
   1.449 +  NS_ENSURE_ARG_POINTER(aParams);
   1.450 +  NS_ENSURE_ARG_POINTER(aCommandName);
   1.451 +  nsresult rv;
   1.452 +
   1.453 +  if (!nsCRT::strcmp(aCommandName, "obs_documentCreated"))
   1.454 +  {
   1.455 +    uint32_t editorStatus = nsIEditingSession::eEditorErrorUnknown;
   1.456 +
   1.457 +    nsCOMPtr<nsIEditingSession> editingSession = do_QueryInterface(refCon);
   1.458 +    if (editingSession)
   1.459 +    {
   1.460 +      // refCon is initially set to nsIEditingSession until editor
   1.461 +      //  is successfully created and source doc is loaded
   1.462 +      // Embedder gets error status if this fails
   1.463 +      // If called before startup is finished, 
   1.464 +      //    status = eEditorCreationInProgress
   1.465 +      rv = editingSession->GetEditorStatus(&editorStatus);
   1.466 +      NS_ENSURE_SUCCESS(rv, rv);
   1.467 +    }
   1.468 +    else
   1.469 +    {
   1.470 +      // If refCon is an editor, then everything started up OK!
   1.471 +      nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
   1.472 +      if (editor)
   1.473 +        editorStatus = nsIEditingSession::eEditorOK;
   1.474 +    }
   1.475 +
   1.476 +    // Note that if refCon is not-null, but is neither
   1.477 +    // an nsIEditingSession or nsIEditor, we return "eEditorErrorUnknown"
   1.478 +    aParams->SetLongValue(STATE_DATA, editorStatus);
   1.479 +    return NS_OK;
   1.480 +  }  
   1.481 +  else if (!nsCRT::strcmp(aCommandName, "obs_documentLocationChanged"))
   1.482 +  {
   1.483 +    nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
   1.484 +    if (editor)
   1.485 +    {
   1.486 +      nsCOMPtr<nsIDOMDocument> domDoc;
   1.487 +      editor->GetDocument(getter_AddRefs(domDoc));
   1.488 +      nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
   1.489 +      NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
   1.490 +
   1.491 +      nsIURI *uri = doc->GetDocumentURI();
   1.492 +      NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
   1.493 +
   1.494 +      return aParams->SetISupportsValue(STATE_DATA, (nsISupports*)uri);
   1.495 +    }
   1.496 +    return NS_OK;
   1.497 +  }  
   1.498 +
   1.499 +  return NS_ERROR_NOT_IMPLEMENTED;
   1.500 +}

mercurial