1// Copyright 2016 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "fpdfsdk/javascript/Annot.h"
8
9#include "fpdfsdk/javascript/JS_Define.h"
10#include "fpdfsdk/javascript/JS_Object.h"
11#include "fpdfsdk/javascript/JS_Value.h"
12#include "fpdfsdk/javascript/cjs_event_context.h"
13
14namespace {
15
16CPDFSDK_BAAnnot* ToBAAnnot(CPDFSDK_Annot* annot) {
17  return static_cast<CPDFSDK_BAAnnot*>(annot);
18}
19
20}  // namespace
21
22JSConstSpec CJS_Annot::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}};
23
24JSPropertySpec CJS_Annot::PropertySpecs[] = {
25    {"hidden", get_hidden_static, set_hidden_static},
26    {"name", get_name_static, set_name_static},
27    {"type", get_type_static, set_type_static},
28    {0, 0, 0}};
29
30JSMethodSpec CJS_Annot::MethodSpecs[] = {{0, 0}};
31
32IMPLEMENT_JS_CLASS(CJS_Annot, Annot)
33
34Annot::Annot(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {}
35
36Annot::~Annot() {}
37
38bool Annot::hidden(CJS_Runtime* pRuntime,
39                   CJS_PropValue& vp,
40                   CFX_WideString& sError) {
41  if (vp.IsGetting()) {
42    if (!m_pAnnot) {
43      sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
44      return false;
45    }
46    CPDF_Annot* pPDFAnnot = ToBAAnnot(m_pAnnot.Get())->GetPDFAnnot();
47    vp << CPDF_Annot::IsAnnotationHidden(pPDFAnnot->GetAnnotDict());
48    return true;
49  }
50
51  bool bHidden;
52  vp >> bHidden;  // May invalidate m_pAnnot.
53  if (!m_pAnnot) {
54    sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
55    return false;
56  }
57
58  uint32_t flags = ToBAAnnot(m_pAnnot.Get())->GetFlags();
59  if (bHidden) {
60    flags |= ANNOTFLAG_HIDDEN;
61    flags |= ANNOTFLAG_INVISIBLE;
62    flags |= ANNOTFLAG_NOVIEW;
63    flags &= ~ANNOTFLAG_PRINT;
64  } else {
65    flags &= ~ANNOTFLAG_HIDDEN;
66    flags &= ~ANNOTFLAG_INVISIBLE;
67    flags &= ~ANNOTFLAG_NOVIEW;
68    flags |= ANNOTFLAG_PRINT;
69  }
70  ToBAAnnot(m_pAnnot.Get())->SetFlags(flags);
71  return true;
72}
73
74bool Annot::name(CJS_Runtime* pRuntime,
75                 CJS_PropValue& vp,
76                 CFX_WideString& sError) {
77  if (vp.IsGetting()) {
78    if (!m_pAnnot) {
79      sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
80      return false;
81    }
82    vp << ToBAAnnot(m_pAnnot.Get())->GetAnnotName();
83    return true;
84  }
85
86  CFX_WideString annotName;
87  vp >> annotName;  // May invalidate m_pAnnot.
88  if (!m_pAnnot) {
89    sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
90    return false;
91  }
92
93  ToBAAnnot(m_pAnnot.Get())->SetAnnotName(annotName);
94  return true;
95}
96
97bool Annot::type(CJS_Runtime* pRuntime,
98                 CJS_PropValue& vp,
99                 CFX_WideString& sError) {
100  if (vp.IsSetting()) {
101    sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
102    return false;
103  }
104  if (!m_pAnnot) {
105    sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
106    return false;
107  }
108  vp << CPDF_Annot::AnnotSubtypeToString(
109      ToBAAnnot(m_pAnnot.Get())->GetAnnotSubtype());
110  return true;
111}
112
113void Annot::SetSDKAnnot(CPDFSDK_BAAnnot* annot) {
114  m_pAnnot.Reset(annot);
115}
116