summaryrefslogtreecommitdiffstats
path: root/writerfilter/source/dmapper/DocumentProtection.cxx
blob: dddf964c4022a1d8be4d7c58a6546d7500d72771 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "DocumentProtection.hxx"
#include "TagLogger.hxx"
#include <vector>
#include <comphelper/sequence.hxx>

using namespace com::sun::star;

namespace writerfilter::dmapper
{
DocumentProtection::DocumentProtection()
    : LoggedProperties("DocumentProtection")
    , m_nEdit(
          NS_ooxml::
              LN_Value_doc_ST_DocProtect_none) // Specifies that no editing restrictions have been applied to the document
    , m_bProtectForm(false)
    , m_bRedlineProtection(false)
    , m_bReadOnly(false)
    , m_bEnforcement(false)
    , m_bFormatting(false)
    , m_nCryptProviderType(NS_ooxml::LN_Value_doc_ST_CryptProv_rsaAES)
    , m_sCryptAlgorithmClass("hash")
    , m_sCryptAlgorithmType("typeAny")
    , m_CryptSpinCount(0)
{
}

DocumentProtection::~DocumentProtection() {}

void DocumentProtection::lcl_attribute(Id nName, Value& val)
{
    int nIntValue = val.getInt();
    OUString sStringValue = val.getString();

    switch (nName)
    {
        case NS_ooxml::LN_CT_DocProtect_edit: // 92037
            m_nEdit = nIntValue;
            // multiple DocProtect_edits should not exist. If they do, last one wins
            m_bRedlineProtection = false;
            m_bProtectForm = false;
            m_bReadOnly = false;
            switch (nIntValue)
            {
                case NS_ooxml::LN_Value_doc_ST_DocProtect_trackedChanges:
                {
                    m_bRedlineProtection = true;
                    m_sRedlineProtectionKey = m_sHash;
                    break;
                }
                case NS_ooxml::LN_Value_doc_ST_DocProtect_forms:
                    m_bProtectForm = true;
                    break;
                case NS_ooxml::LN_Value_doc_ST_DocProtect_readOnly:
                    m_bReadOnly = true;
                    break;
            }
            break;
        case NS_ooxml::LN_CT_DocProtect_enforcement: // 92039
            m_bEnforcement = (nIntValue != 0);
            break;
        case NS_ooxml::LN_CT_DocProtect_formatting: // 92038
            m_bFormatting = (nIntValue != 0);
            break;
        case NS_ooxml::LN_AG_Password_cryptProviderType: // 92025
            m_nCryptProviderType = nIntValue;
            break;
        case NS_ooxml::LN_AG_Password_cryptAlgorithmClass: // 92026
            if (nIntValue == NS_ooxml::LN_Value_doc_ST_AlgClass_hash) // 92023
                m_sCryptAlgorithmClass = "hash";
            break;
        case NS_ooxml::LN_AG_Password_cryptAlgorithmType: // 92027
            if (nIntValue == NS_ooxml::LN_Value_doc_ST_AlgType_typeAny) // 92024
                m_sCryptAlgorithmType = "typeAny";
            break;
        case NS_ooxml::LN_AG_Password_cryptAlgorithmSid: // 92028
            m_sCryptAlgorithmSid = sStringValue;
            break;
        case NS_ooxml::LN_AG_Password_cryptSpinCount: // 92029
            m_CryptSpinCount = nIntValue;
            break;
        case NS_ooxml::LN_AG_Password_hash: // 92035
            m_sHash = sStringValue;
            break;
        case NS_ooxml::LN_AG_Password_salt: // 92036
            m_sSalt = sStringValue;
            break;
        default:
        {
#ifdef DBG_UTIL
            TagLogger::getInstance().element("unhandled");
#endif
        }
    }
}

void DocumentProtection::lcl_sprm(Sprm& /*rSprm*/) {}

uno::Sequence<beans::PropertyValue> DocumentProtection::toSequence() const
{
    std::vector<beans::PropertyValue> documentProtection;

    if (enabled())
    {
        // w:edit
        {
            beans::PropertyValue aValue;
            aValue.Name = "edit";

            switch (m_nEdit)
            {
                case NS_ooxml::LN_Value_doc_ST_DocProtect_none:
                    aValue.Value <<= OUString("none");
                    break;
                case NS_ooxml::LN_Value_doc_ST_DocProtect_readOnly:
                    aValue.Value <<= OUString("readOnly");
                    break;
                case NS_ooxml::LN_Value_doc_ST_DocProtect_comments:
                    aValue.Value <<= OUString("comments");
                    break;
                case NS_ooxml::LN_Value_doc_ST_DocProtect_trackedChanges:
                    aValue.Value <<= OUString("trackedChanges");
                    break;
                case NS_ooxml::LN_Value_doc_ST_DocProtect_forms:
                    aValue.Value <<= OUString("forms");
                    break;
                default:
                {
#ifdef DBG_UTIL
                    TagLogger::getInstance().element("unhandled");
#endif
                }
            }

            documentProtection.push_back(aValue);
        }

        // w:enforcement
        if (m_bEnforcement)
        {
            beans::PropertyValue aValue;
            aValue.Name = "enforcement";
            aValue.Value <<= OUString("1");
            documentProtection.push_back(aValue);
        }

        // w:formatting
        if (m_bFormatting)
        {
            beans::PropertyValue aValue;
            aValue.Name = "formatting";
            aValue.Value <<= OUString("1");
            documentProtection.push_back(aValue);
        }

        // w:cryptProviderType
        {
            beans::PropertyValue aValue;
            aValue.Name = "cryptProviderType";
            if (m_nCryptProviderType == NS_ooxml::LN_Value_doc_ST_CryptProv_rsaAES)
                aValue.Value <<= OUString("rsaAES");
            else if (m_nCryptProviderType == NS_ooxml::LN_Value_doc_ST_CryptProv_rsaFull)
                aValue.Value <<= OUString("rsaFull");
            documentProtection.push_back(aValue);
        }

        // w:cryptAlgorithmClass
        {
            beans::PropertyValue aValue;
            aValue.Name = "cryptAlgorithmClass";
            aValue.Value <<= m_sCryptAlgorithmClass;
            documentProtection.push_back(aValue);
        }

        // w:cryptAlgorithmType
        {
            beans::PropertyValue aValue;
            aValue.Name = "cryptAlgorithmType";
            aValue.Value <<= m_sCryptAlgorithmType;
            documentProtection.push_back(aValue);
        }

        // w:cryptAlgorithmSid
        {
            beans::PropertyValue aValue;
            aValue.Name = "cryptAlgorithmSid";
            aValue.Value <<= m_sCryptAlgorithmSid;
            documentProtection.push_back(aValue);
        }

        // w:cryptSpinCount
        {
            beans::PropertyValue aValue;
            aValue.Name = "cryptSpinCount";
            aValue.Value <<= OUString::number(m_CryptSpinCount);
            documentProtection.push_back(aValue);
        }

        // w:hash
        {
            beans::PropertyValue aValue;
            aValue.Name = "hash";
            aValue.Value <<= m_sHash;
            documentProtection.push_back(aValue);
        }

        // w:salt
        {
            beans::PropertyValue aValue;
            aValue.Name = "salt";
            aValue.Value <<= m_sSalt;
            documentProtection.push_back(aValue);
        }
    }

    return comphelper::containerToSequence(documentProtection);
}

} //namespace writerfilter::dmapper

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */