summaryrefslogtreecommitdiffstats
path: root/binfilter/bf_sc/source/ui/docshell/sc_tablink.cxx
blob: 21ba02f247def2b9b4a38274614e8b0d68aeb357 (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#ifdef _MSC_VER
#pragma optimize("",off)
#endif

#ifdef _MSC_VER
#pragma hdrstop
#endif


#include <bf_sfx2/app.hxx>
#include <bf_svtools/itemset.hxx>
#include <bf_svtools/stritem.hxx>
#include <bf_sfx2/docfile.hxx>
#include <bf_sfx2/fcontnr.hxx>
#include <bf_svx/linkmgr.hxx>
#include <unotools/transliterationwrapper.hxx>

#include "tablink.hxx"

#include "docsh.hxx"
#include "globstr.hrc"
#include "hints.hxx"
namespace binfilter {

/*N*/ TYPEINIT1(ScTableLink, ::binfilter::SvBaseLink);

//------------------------------------------------------------------------

/*N*/ ScTableLink::ScTableLink(ScDocShell* pDocSh, const String& rFile,
/*N*/                           const String& rFilter, const String& rOpt,
/*N*/                           ULONG nRefresh ):
/*N*/   ::binfilter::SvBaseLink(::binfilter::LINKUPDATE_ONCALL,FORMAT_FILE),
/*N*/   ScRefreshTimer( nRefresh ),
/*N*/   pDocShell(pDocSh),
/*N*/   aFileName(rFile),
/*N*/   aFilterName(rFilter),
/*N*/   aOptions(rOpt),
/*N*/   bInCreate( FALSE ),
/*M*/   bInEdit( FALSE ),
/*N*/   bAddUndo( TRUE ),
/*N*/   bDoPaint( TRUE )
/*N*/ {
/*N*/ }

/*N*/ ScTableLink::ScTableLink(SfxObjectShell* pShell, const String& rFile,
/*N*/                           const String& rFilter, const String& rOpt,
/*N*/                           ULONG nRefresh ):
/*N*/   ::binfilter::SvBaseLink(::binfilter::LINKUPDATE_ONCALL,FORMAT_FILE),
/*N*/   ScRefreshTimer( nRefresh ),
/*N*/   pDocShell((ScDocShell*)pShell),
/*N*/   aFileName(rFile),
/*N*/   aFilterName(rFilter),
/*N*/   aOptions(rOpt),
/*N*/   bInCreate( FALSE ),
/*M*/   bInEdit( FALSE ),
/*N*/   bAddUndo( TRUE ),
/*N*/   bDoPaint( TRUE )
/*N*/ {
/*N*/   SetRefreshHandler( LINK( this, ScTableLink, RefreshHdl ) );
/*N*/   SetRefreshControl( pDocShell->GetDocument()->GetRefreshTimerControlAddress() );
/*N*/ }

/*N*/ ScTableLink::~ScTableLink()
/*N*/ {
/*N*/   // Verbindung aufheben
/*N*/
/*N*/   StopRefreshTimer();
/*N*/   String aEmpty;
/*N*/   ScDocument* pDoc = pDocShell->GetDocument();
/*N*/   USHORT nCount = pDoc->GetTableCount();
/*N*/   for (USHORT nTab=0; nTab<nCount; nTab++)
/*N*/       if (pDoc->IsLinked(nTab) && pDoc->GetLinkDoc(nTab)==aFileName)
/*N*/           pDoc->SetLink( nTab, SC_LINK_NONE, aEmpty, aEmpty, aEmpty, aEmpty, 0 );
/*N*/ }


/*N*/ void ScTableLink::DataChanged( const String&,
/*N*/                                       const ::com::sun::star::uno::Any& )
/*N*/ {
/*N*/   SvxLinkManager* pLinkManager=pDocShell->GetDocument()->GetLinkManager();
/*N*/   if (pLinkManager!=NULL)
/*N*/   {
/*N*/       String aFile;
/*N*/       String aFilter;
/*N*/       pLinkManager->GetDisplayNames( this,0,&aFile,NULL,&aFilter);
/*N*/
/*N*/       //  the file dialog returns the filter name with the application prefix
/*N*/       //  -> remove prefix
/*N*/       ScDocumentLoader::RemoveAppPrefix( aFilter );
/*N*/
/*N*/       if (!bInCreate)
/*N*/           Refresh( aFile, aFilter, NULL, GetRefreshDelay() ); // don't load twice
/*N*/   }
/*N*/ }



/*N*/ BOOL ScTableLink::Refresh(const String& rNewFile, const String& rNewFilter,
/*N*/                           const String* pNewOptions, ULONG nNewRefresh )
/*N*/ {
/*N*/   //  Dokument laden
/*N*/
/*N*/   if (!rNewFile.Len() || !rNewFilter.Len())
/*N*/       return FALSE;
/*N*/
/*N*/   String aNewUrl( ScGlobal::GetAbsDocName( rNewFile, pDocShell ) );
/*N*/   BOOL bNewUrlName = (aNewUrl != aFileName);
/*N*/
/*N*/   const SfxFilter* pFilter = SFX_APP()->GetFilter( pDocShell->GetFactory(), rNewFilter );
/*N*/   if (!pFilter)
/*N*/       return FALSE;
/*N*/
/*N*/   ScDocument* pDoc = pDocShell->GetDocument();
/*N*/   pDoc->SetInLinkUpdate( TRUE );
/*N*/
/*N*/   //  wenn neuer Filter ausgewaehlt wurde, Optionen vergessen
/*N*/   if ( rNewFilter != aFilterName )
/*?*/       aOptions.Erase();
/*N*/   if ( pNewOptions )                  // Optionen hart angegeben?
/*N*/       aOptions = *pNewOptions;
/*N*/
/*N*/   //  ItemSet immer anlegen, damit die DocShell die Optionen setzen kann
/*N*/   SfxItemSet* pSet = new SfxAllItemSet( SFX_APP()->GetPool() );
/*N*/   if ( aOptions.Len() )
/*N*/       pSet->Put( SfxStringItem( SID_FILE_FILTEROPTIONS, aOptions ) );
/*N*/
/*N*/   SfxMedium* pMed = new SfxMedium(aNewUrl, STREAM_STD_READ, FALSE, pFilter, pSet);
/*N*/
/*M*/   if ( bInEdit )                              // only if using the edit dialog,
/*M*/       pMed->UseInteractionHandler( TRUE );    // enable the filter options dialog
/*M*/
/*N*/   ScDocShell* pSrcShell = new ScDocShell(SFX_CREATE_MODE_INTERNAL);
/*N*/   SvEmbeddedObjectRef aRef = pSrcShell;
/*N*/   pSrcShell->DoLoad(pMed);
/*N*/
/*N*/   // Optionen koennten gesetzt worden sein
/*N*/   String aNewOpt = ScDocumentLoader::GetOptions(*pMed);
/*N*/   if (!aNewOpt.Len())
/*N*/       aNewOpt = aOptions;
/*N*/
/*N*/   //  Tabellen kopieren
/*N*/
/*N*/   ScDocShellModificator aModificator( *pDocShell );
/*N*/
/*N*/   BOOL bNotFound = FALSE;
/*N*/   ScDocument* pSrcDoc = pSrcShell->GetDocument();
/*N*/
/*N*/   //  #74835# from text filters that don't set the table name,
/*N*/   //  use the one table regardless of link table name
/*N*/   BOOL bAutoTab = (pSrcDoc->GetTableCount() == 1) &&
/*N*/                   ScDocShell::HasAutomaticTableName( rNewFilter );
/*N*/
/*N*/   USHORT nCount = pDoc->GetTableCount();
/*N*/   for (USHORT nTab=0; nTab<nCount; nTab++)
/*N*/   {
/*N*/       BYTE nMode = pDoc->GetLinkMode(nTab);
/*N*/       if (nMode && pDoc->GetLinkDoc(nTab)==aFileName)
/*N*/       {
/*N*/           String aTabName = pDoc->GetLinkTab(nTab);
/*N*/
/*N*/           //  Tabellenname einer ExtDocRef anpassen
/*N*/
/*N*/           if ( bNewUrlName && nMode == SC_LINK_VALUE )
/*N*/           {
/*N*/               String aName;
/*N*/               pDoc->GetName( nTab, aName );
/*N*/                 if ( ScGlobal::pTransliteration->isEqual(
/*N*/                         ScGlobal::GetDocTabName( aFileName, aTabName ), aName ) )
/*N*/               {
/*N*/                   pDoc->RenameTab( nTab,
/*N*/                       ScGlobal::GetDocTabName( aNewUrl, aTabName ),
/*N*/                       FALSE, TRUE );  // kein RefUpdate, kein ValidTabName
/*N*/               }
/*N*/           }
/*N*/
/*N*/           //  kopieren
/*N*/
/*N*/           USHORT nSrcTab = 0;
/*N*/           BOOL bFound = TRUE;         // kein Tab-Name angegeben: immer die erste
/*N*/           if ( aTabName.Len() && !bAutoTab )
/*N*/               bFound = pSrcDoc->GetTable( aTabName, nSrcTab );
/*N*/           if (bFound)
/*N*/               pDoc->TransferTab( pSrcDoc, nSrcTab, nTab, FALSE,       // nicht neu einfuegen
/*N*/                                       (nMode == SC_LINK_VALUE) );     // nur Werte?
/*N*/           else
/*N*/           {
/*N*/               pDoc->DeleteAreaTab( 0,0,MAXCOL,MAXROW, nTab, IDF_ALL );
/*N*/               //  Fehler eintragen
/*N*/               pDoc->SetString( 0,0,nTab, ScGlobal::GetRscString(STR_LINKERROR) );
/*N*/               pDoc->SetString( 0,1,nTab, ScGlobal::GetRscString(STR_LINKERRORFILE) );
/*N*/               pDoc->SetString( 1,1,nTab, aNewUrl );
/*N*/               pDoc->SetString( 0,2,nTab, ScGlobal::GetRscString(STR_LINKERRORTAB) );
/*N*/               pDoc->SetString( 1,2,nTab, aTabName );
/*N*/
/*N*/               bNotFound = TRUE;
/*N*/           }
/*N*/
/*N*/           if ( bNewUrlName || rNewFilter != aFilterName ||
/*N*/                   aNewOpt != aOptions || pNewOptions ||
/*N*/                   nNewRefresh != GetRefreshDelay() )
/*?*/               pDoc->SetLink( nTab, nMode, aNewUrl, rNewFilter, aNewOpt,
/*?*/                   aTabName, nNewRefresh );
/*N*/       }
/*N*/   }
/*N*/
/*N*/   //  neue Einstellungen merken
/*N*/
/*N*/   if ( bNewUrlName )
/*N*/       aFileName = aNewUrl;
/*N*/   if ( rNewFilter != aFilterName )
/*N*/       aFilterName = rNewFilter;
/*N*/   if ( aNewOpt != aOptions )
/*N*/       aOptions = aNewOpt;
/*N*/
/*N*/   //  aufraeumen
/*N*/
/*N*/   aRef->DoClose();
/*N*/
/*N*/   //  Paint (koennen mehrere Tabellen sein)
/*N*/
/*N*/   if (bDoPaint)
/*N*/   {
/*?*/       pDocShell->PostPaint( ScRange(0,0,0,MAXCOL,MAXROW,MAXTAB),
/*?*/                               PAINT_GRID | PAINT_TOP | PAINT_LEFT );
/*?*/       aModificator.SetDocumentModified();
/*N*/   }
/*N*/
/*N*/   if (bNotFound)
/*N*/   {
/*N*/       //! Fehler ausgeben ?
/*N*/   }
/*N*/
/*N*/   pDoc->SetInLinkUpdate( FALSE );
/*N*/
/*N*/   //  notify Uno objects (for XRefreshListener)
/*N*/   //! also notify Uno objects if file name was changed!
/*N*/   ScLinkRefreshedHint aHint;
/*N*/   aHint.SetSheetLink( aFileName );
/*N*/   pDoc->BroadcastUno( aHint );
/*N*/
/*N*/   return TRUE;
/*N*/ }


/*N*/ IMPL_LINK( ScTableLink, RefreshHdl, ScTableLink*, EMPTYARG )
/*N*/ {
/*N*/   long nRes = Refresh( aFileName, aFilterName, NULL, GetRefreshDelay() ) != 0;
/*N*/   return nRes;
/*N*/ }


// === ScDocumentLoader ==================================================

/*N*/ String ScDocumentLoader::GetOptions( SfxMedium& rMedium )     // static
/*N*/ {
/*N*/   SfxItemSet* pSet = rMedium.GetItemSet();
/*N*/   const SfxPoolItem* pItem;
/*N*/   if ( pSet && SFX_ITEM_SET == pSet->GetItemState( SID_FILE_FILTEROPTIONS, TRUE, &pItem ) )
/*N*/       return ((const SfxStringItem*)pItem)->GetValue();
/*N*/
/*N*/   return EMPTY_STRING;
/*N*/ }

/*N*/ void ScDocumentLoader::GetFilterName( const String& rFileName,
/*N*/                                   String& rFilter, String& rOptions, BOOL bWithContent )  // static
/*N*/ {
/*N*/   TypeId aScType = TYPE(ScDocShell);
/*N*/   SfxObjectShell* pDocSh = SfxObjectShell::GetFirst( &aScType );
/*N*/   while ( pDocSh )
/*N*/   {
/*N*/       if ( pDocSh->HasName() )
/*N*/       {
/*?*/           SfxMedium* pMed = pDocSh->GetMedium();
/*?*/           if ( rFileName == pMed->GetName() )
/*?*/           {
/*?*/               rFilter = pMed->GetFilter()->GetFilterName();
/*?*/               rOptions = GetOptions(*pMed);
/*?*/               return;
/*?*/           }
/*N*/       }
/*N*/       pDocSh = SfxObjectShell::GetNext( *pDocSh, &aScType );
/*N*/   }
/*N*/
/*N*/   //  Filter-Detection
/*N*/
/*N*/   const SfxFilter* pSfxFilter = NULL;
/*N*/   SfxMedium* pMedium = new SfxMedium( rFileName, STREAM_STD_READ, FALSE );
/*N*/   if ( pMedium->GetError() == ERRCODE_NONE )
/*N*/   {
/*N*/       SfxFilterContainer* pContainer = ScDocShell::Factory().GetFilterContainer();
/*N*/       if ( bWithContent && pContainer )
/*N*/       {
/*?*/           // look at file content (call DetectFilter)
/*?*/           pContainer->GetFilter4Content( *pMedium, &pSfxFilter );
/*N*/       }
/*N*/       else
/*N*/       {
/*N*/           SfxFilterMatcher aMatcher( pContainer );
/*N*/           aMatcher.GuessFilter( *pMedium, &pSfxFilter );
/*N*/       }
/*N*/   }
/*N*/
/*N*/   if ( pSfxFilter )
/*N*/       rFilter = pSfxFilter->GetFilterName();
/*N*/   else
/*?*/       rFilter = ScDocShell::GetOwnFilterName();       //  sonst Calc-Datei
/*N*/
/*N*/   delete pMedium;
/*N*/ }

/*N*/ void ScDocumentLoader::RemoveAppPrefix( String& rFilterName )     // static
/*N*/ {
/*N*/   String aAppPrefix = String::CreateFromAscii(RTL_CONSTASCII_STRINGPARAM( STRING_SCAPP ));
/*N*/   aAppPrefix.AppendAscii(RTL_CONSTASCII_STRINGPARAM( ": " ));
/*N*/   xub_StrLen nPreLen = aAppPrefix.Len();
/*N*/   if ( rFilterName.Copy(0,nPreLen) == aAppPrefix )
/*?*/       rFilterName.Erase(0,nPreLen);
/*N*/ }

/*M*/ ScDocumentLoader::ScDocumentLoader( const String& rFileName,
/*M*/                                   String& rFilterName, String& rOptions,
/*M*/                                   UINT32 /*nRekCnt*/, BOOL bWithInteraction ) :
/*M*/       pDocShell(0),
/*M*/       pMedium(0)
/*M*/ {
/*M*/   if ( !rFilterName.Len() )
/*M*/       GetFilterName( rFileName, rFilterName, rOptions );
/*M*/
/*M*/   const SfxFilter* pFilter = SFX_APP()->GetFilter(
/*M*/                               ScDocShell::Factory(), rFilterName );
/*M*/
/*M*/   //  ItemSet immer anlegen, damit die DocShell die Optionen setzen kann
/*M*/   SfxItemSet* pSet = new SfxAllItemSet( SFX_APP()->GetPool() );
/*M*/   if ( rOptions.Len() )
/*M*/       pSet->Put( SfxStringItem( SID_FILE_FILTEROPTIONS, rOptions ) );
/*M*/
/*M*/   pMedium = new SfxMedium( rFileName, STREAM_STD_READ, FALSE, pFilter, pSet );
/*M*/   if ( pMedium->GetError() != ERRCODE_NONE )
/*M*/       return ;
/*M*/
/*M*/   if ( bWithInteraction )
/*M*/       pMedium->UseInteractionHandler( TRUE ); // to enable the filter options dialog
/*M*/
/*M*/   pDocShell = new ScDocShell( SFX_CREATE_MODE_INTERNAL );
/*M*/   aRef = pDocShell;
/*M*/
/*M*/   pDocShell->DoLoad( pMedium );
/*M*/
/*M*/   String aNew = GetOptions(*pMedium);         // Optionen werden beim Laden per Dialog gesetzt
/*M*/   if (aNew.Len() && aNew != rOptions)
/*M*/       rOptions = aNew;
/*M*/ }
/*N*/
/*N*/ ScDocumentLoader::~ScDocumentLoader()
/*N*/ {
/*N*/   if ( aRef.Is() )
/*N*/       aRef->DoClose();
/*N*/   else if ( pMedium )
/*N*/       delete pMedium;
/*N*/ }

/*N*/ ScDocument* ScDocumentLoader::GetDocument()
/*N*/ {
/*N*/   return pDocShell ? pDocShell->GetDocument() : 0;
/*N*/ }

/*N*/ BOOL ScDocumentLoader::IsError() const
/*N*/ {
/*N*/   if ( pDocShell && pMedium )
/*N*/       return pMedium->GetError() != ERRCODE_NONE;
/*N*/   else
/*N*/       return TRUE;
/*N*/ }
}

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