summaryrefslogtreecommitdiff
path: root/fpicker/source/office/ServerDetailsControls.cxx
blob: bcc9cd3451afb518b476a1ccb9adcb1d7aff415b (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License or as specified alternatively below. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Major Contributor(s):
 * [ Copyright (C) 2012 SUSE <cbosdonnat@suse.com> (initial developer) ]
 *
 * All Rights Reserved.
 *
 * For minor contributions see the git repository.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
 * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
 * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
 * instead of those above.
 */

#include <rtl/uri.hxx>

#include "PlaceEditDialog.hrc"

#include "fpsofficeResMgr.hxx"
#include "PlaceEditDialog.hxx"
#include "ServerDetailsControls.hxx"

using namespace std;

DetailsContainer::DetailsContainer( ) :
    m_aControls( ),
    m_bShown( false )
{
}

DetailsContainer::~DetailsContainer( )
{
    m_aControls.clear( );
}

void DetailsContainer::addControl( sal_uInt16 nId, Control* pControl )
{
    m_aControls.insert( pair< sal_uInt16, Control* >( nId, pControl ) );
    pControl->Show( m_bShown );

    if ( pControl->GetType( ) == WINDOW_EDIT )
        static_cast< Edit* >( pControl )->SetModifyHdl( LINK( this, DetailsContainer, ValueChangeHdl ) );
    else if ( pControl->GetType( ) == WINDOW_NUMERICFIELD )
        static_cast< NumericField* >( pControl )->SetModifyHdl( LINK( this, DetailsContainer, ValueChangeHdl ) );
    else if ( pControl->GetType( ) == WINDOW_CHECKBOX )
        static_cast< CheckBox* >( pControl )->SetToggleHdl( LINK( this, DetailsContainer, ValueChangeHdl ) );
}

Control* DetailsContainer::getControl( sal_uInt16 nId )
{
    Control* pControl = NULL;
    map< sal_uInt16, Control* >::iterator it = m_aControls.find( nId );
    if ( it != m_aControls.end( ) )
        pControl = it->second;
    return pControl;
}

Rectangle DetailsContainer::getBounds( )
{
    Rectangle aBounds;
    for ( map< sal_uInt16, Control* >::iterator it = m_aControls.begin( ); it != m_aControls.end( ); ++it )
    {
        Rectangle aControlBounds( it->second->GetPosPixel(), it->second->GetSizePixel() );
        aBounds = aBounds.GetUnion( aControlBounds );
    }

    return aBounds;
}

void DetailsContainer::show( bool bShow )
{
    m_bShown = bShow;
    for ( map< sal_uInt16, Control* >::iterator it = m_aControls.begin( ); it != m_aControls.end( ); ++it )
    {
        it->second->Show( m_bShown );
    }
}

INetURLObject DetailsContainer::getUrl( )
{
    // Don't use that class directly: make it smarter by subclassing it.
    return INetURLObject( );
}

bool DetailsContainer::setUrl( const INetURLObject& )
{
    // That class doesn't contain any logic... it defers the dirty work
    // to the sub classes.
    return false;
}

void DetailsContainer::notifyChange( )
{
    m_aChangeHdl.Call( this );
}

IMPL_LINK( DetailsContainer, ValueChangeHdl, void *, EMPTYARG )
{
    notifyChange( );
    return 0;
}

HostDetailsContainer::HostDetailsContainer( sal_uInt16 nPort, rtl::OUString sScheme ) :
    DetailsContainer( ),
    m_nDefaultPort( nPort ),
    m_sScheme( sScheme )
{
}

void HostDetailsContainer::show( bool bShow )
{
    DetailsContainer::show( bShow );
    if ( bShow )
        static_cast< NumericField* >( getControl( ED_ADDPLACE_PORT ) )->SetValue( m_nDefaultPort );
}

INetURLObject HostDetailsContainer::getUrl( )
{
    rtl::OUString sHost = rtl::OUString( static_cast< Edit* >( getControl( ED_ADDPLACE_HOST ) )->GetText() ).trim( );
    sal_Int64 nPort = static_cast< NumericField* >( getControl( ED_ADDPLACE_PORT ) )->GetValue();
    rtl::OUString sPath = rtl::OUString( static_cast< Edit* >( getControl( ED_ADDPLACE_PATH ) )->GetText() ).trim( );

    rtl::OUString sUrl;
    if ( !sHost.isEmpty( ) )
    {
        sUrl = m_sScheme + "://" + sHost;
        if ( nPort != m_nDefaultPort )
            sUrl += ":" + rtl::OUString::valueOf( nPort );
        if ( !sPath.isEmpty( ) )
            if ( sPath.indexOf( "/" ) != 0 )
                sUrl += "/";
            sUrl += sPath;
    }

    return INetURLObject( sUrl );
}

bool HostDetailsContainer::setUrl( const INetURLObject& rUrl )
{
    bool bSuccess = false;

    bSuccess = verifyScheme( INetURLObject::GetScheme( rUrl.GetProtocol( ) ) );

    if ( bSuccess )
    {
        static_cast< Edit* >( getControl( ED_ADDPLACE_HOST ) )->SetText( rUrl.GetHost( ) );
        static_cast< NumericField* >( getControl( ED_ADDPLACE_PORT ) )->SetValue( rUrl.GetPort( ) );
        static_cast< Edit* >( getControl( ED_ADDPLACE_PATH ) )->SetText( rUrl.GetURLPath() );
    }

    return bSuccess;
}

bool HostDetailsContainer::verifyScheme( const rtl::OUString& sScheme )
{
    return sScheme.equals( m_sScheme + "://" );
}

DavDetailsContainer::DavDetailsContainer( ) :
    HostDetailsContainer( 80, "http" )
{
}

void DavDetailsContainer::addControl( sal_uInt16 nId, Control* pControl )
{
    DetailsContainer::addControl( nId, pControl );

    // Add listener on CB_ADDPLACE_DAVS
    if ( nId == CB_ADDPLACE_DAVS )
        static_cast< CheckBox* >( pControl )->SetToggleHdl( LINK( this, DavDetailsContainer, ToggledDavsHdl ) );
}

void DavDetailsContainer::show( bool bShow )
{
    HostDetailsContainer::show( bShow );
    if ( bShow )
        static_cast< CheckBox* >( getControl( CB_ADDPLACE_DAVS ) )->Check( false );
}

bool DavDetailsContainer::verifyScheme( const rtl::OUString& rScheme )
{
    bool bValid = false;
    if ( rScheme.equals( "http://" ) )
    {
        bValid = true;
        static_cast< CheckBox* >( getControl( CB_ADDPLACE_DAVS ) )->Check( false );
    }
    else if ( rScheme.equals( "https://" ) )
    {
        bValid = true;
        static_cast< CheckBox* >( getControl( CB_ADDPLACE_DAVS ) )->Check( true );
    }
    return bValid;
}

IMPL_LINK( DavDetailsContainer, ToggledDavsHdl, CheckBox*, pCheckBox )
{
    // Change default port if needed
    sal_Bool bCheckedDavs = pCheckBox->IsChecked();
    NumericField* pPortField = static_cast< NumericField* >( getControl( ED_ADDPLACE_PORT ) );
    if ( pPortField->GetValue() == 80 && bCheckedDavs == sal_True)
        pPortField->SetValue( 443 );
    else if ( pPortField->GetValue() == 443 && bCheckedDavs == sal_False )
        pPortField->SetValue( 80 );

    rtl::OUString sScheme( "http" );
    if ( bCheckedDavs )
        sScheme = "https";
    setScheme( sScheme );

    notifyChange( );

    return 0;
}

INetURLObject SmbDetailsContainer::getUrl( )
{
    rtl::OUString sHost = rtl::OUString( static_cast< Edit* >( getControl( ED_ADDPLACE_SMBHOST ) )->GetText() ).trim( );
    rtl::OUString sShare = rtl::OUString( static_cast< Edit* >( getControl( ED_ADDPLACE_SHARE ) )->GetText() ).trim( );
    rtl::OUString sPath = rtl::OUString( static_cast< Edit* >( getControl( ED_ADDPLACE_SMBPATH ) )->GetText() ).trim( );

    rtl::OUString sUrl;
    if ( !sHost.isEmpty( ) )
    {
        sUrl = "smb://" + sHost + "/";
        if ( !sShare.isEmpty( ) )
            sUrl += sShare;
        if ( !sPath.isEmpty( ) )
            if ( sPath.indexOf( "/" ) != 0 )
                sUrl += "/";
            sUrl += sPath;
    }

    return INetURLObject( sUrl );
}

bool SmbDetailsContainer::setUrl( const INetURLObject& rUrl )
{
    bool bSuccess =  rUrl.GetProtocol() == INET_PROT_SMB;

    if ( bSuccess )
    {
        rtl::OUString sShare = rUrl.getName( 0 );
        rtl::OUString sFullPath = rUrl.GetURLPath( );
        rtl::OUString sPath;
        if ( sFullPath.getLength( ) > sShare.getLength( ) )
        {
            sal_Int32 nPos = sShare.getLength( );
            if ( nPos != 0 )
                ++nPos;
            sPath = sFullPath.copy( nPos );
        }

        static_cast< Edit* >( getControl( ED_ADDPLACE_SMBHOST ) )->SetText( rUrl.GetHost( ) );
        static_cast< Edit* >( getControl( ED_ADDPLACE_SHARE ) )->SetText( sShare );
        static_cast< Edit* >( getControl( ED_ADDPLACE_SMBPATH ) )->SetText( sPath );
    }

    return bSuccess;
}

INetURLObject CmisDetailsContainer::getUrl( )
{
    rtl::OUString sBindingUrl = rtl::OUString( static_cast< Edit* >( getControl( ED_ADDPLACE_CMIS_BINDING ) )->GetText() ).trim( );
    rtl::OUString sRepo = rtl::OUString( static_cast< Edit* >( getControl( ED_ADDPLACE_CMIS_REPOSITORY ) )->GetText() ).trim( );

    rtl::OUString sUrl;
    if ( !sBindingUrl.isEmpty( ) && !sRepo.isEmpty() )
    {
        rtl::OUString sEncodedBinding = rtl::Uri::encode(
                sBindingUrl + "#" + sRepo,
                rtl_UriCharClassRelSegment,
                rtl_UriEncodeKeepEscapes,
                RTL_TEXTENCODING_UTF8 );
        sUrl = "vnd.libreoffice.cmis+atom://" + sEncodedBinding;
        INetURLObject test( sUrl );
    }

    return INetURLObject( sUrl );
}

bool CmisDetailsContainer::setUrl( const INetURLObject& rUrl )
{
    bool bSuccess =  rUrl.GetProtocol() == INET_PROT_CMIS_ATOM;

    if ( bSuccess )
    {
        rtl::OUString sBindingUrl;
        rtl::OUString sRepositoryId;

        rtl::OUString sDecodedHost = rUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET );
        INetURLObject aHostUrl( sDecodedHost );
        sBindingUrl = aHostUrl.GetURLNoMark( );
        sRepositoryId = aHostUrl.GetMark( );

        static_cast< Edit* >( getControl( ED_ADDPLACE_CMIS_BINDING ) )->SetText( sBindingUrl );
        static_cast< Edit* >( getControl( ED_ADDPLACE_CMIS_REPOSITORY ) )->SetText( sRepositoryId );
    }

    return bSuccess;
}


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