1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #include <memory>
21 #include <comphelper/unique_disposing_ptr.hxx>
22 #include <comphelper/processfactory.hxx>
23
24 #include <iderdll.hxx>
25 #include "iderdll2.hxx"
26 #include <iderid.hxx>
27 #include <basidesh.hxx>
28 #include <basobj.hxx>
29 #include "basdoc.hxx"
30 #include "basicmod.hxx"
31
32 #include <basic/sbstar.hxx>
33 #include <com/sun/star/frame/Desktop.hpp>
34 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
35 #include <unotools/resmgr.hxx>
36 #include <sfx2/app.hxx>
37 #include <osl/diagnose.h>
38
39 namespace basctl
40 {
41
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::uno;
44
45 namespace
46 {
47
48 class Dll
49 {
50 Shell* m_pShell;
51 std::unique_ptr<ExtraData> m_xExtraData;
52
53 public:
54 Dll ();
55
GetShell() const56 Shell* GetShell() const { return m_pShell; }
SetShell(Shell * pShell)57 void SetShell (Shell* pShell) { m_pShell = pShell; }
58 ExtraData* GetExtraData ();
59 };
60
61 // Holds a basctl::Dll and release it on exit, or dispose of the
62 //default XComponent, whichever comes first
63 class DllInstance : public comphelper::unique_disposing_solar_mutex_reset_ptr<Dll>
64 {
65 public:
DllInstance()66 DllInstance() : comphelper::unique_disposing_solar_mutex_reset_ptr<Dll>(Reference<lang::XComponent>( frame::Desktop::create(comphelper::getProcessComponentContext()), UNO_QUERY_THROW), new Dll, true)
67 { }
68 };
69
70 struct theDllInstance : public rtl::Static<DllInstance, theDllInstance> { };
71
72 } // namespace
73
EnsureIde()74 void EnsureIde ()
75 {
76 // coverity[side_effect_free : FALSE] - not actually side-effect-free
77 theDllInstance::get();
78 }
79
GetShell()80 Shell* GetShell ()
81 {
82 if (Dll* pDll = theDllInstance::get().get())
83 return pDll->GetShell();
84 return nullptr;
85 }
86
ShellCreated(Shell * pShell)87 void ShellCreated (Shell* pShell)
88 {
89 Dll* pDll = theDllInstance::get().get();
90 if (pDll && !pDll->GetShell())
91 pDll->SetShell(pShell);
92 }
93
ShellDestroyed(Shell const * pShell)94 void ShellDestroyed (Shell const * pShell)
95 {
96 Dll* pDll = theDllInstance::get().get();
97 if (pDll && pDll->GetShell() == pShell)
98 pDll->SetShell(nullptr);
99 }
100
GetExtraData()101 ExtraData* GetExtraData()
102 {
103 if (Dll* pDll = theDllInstance::get().get())
104 return pDll->GetExtraData();
105 return nullptr;
106 }
107
IDEResId(const char * pId)108 OUString IDEResId(const char *pId)
109 {
110 return Translate::get(pId, SfxApplication::GetModule(SfxToolsModule::Basic)->GetResLocale());
111 }
112
113 namespace
114 {
115
Dll()116 Dll::Dll () :
117 m_pShell(nullptr)
118 {
119 SfxObjectFactory& rFactory = DocShell::Factory();
120
121 auto pModule = std::make_unique<Module>("basctl", &rFactory);
122 SfxModule* pMod = pModule.get();
123 SfxApplication::SetModule(SfxToolsModule::Basic, std::move(pModule));
124
125 GetExtraData(); // to cause GlobalErrorHdl to be set
126
127 rFactory.SetDocumentServiceName( "com.sun.star.script.BasicIDE" );
128
129 DocShell::RegisterInterface( pMod );
130 Shell::RegisterFactory( SVX_INTERFACE_BASIDE_VIEWSH );
131 Shell::RegisterInterface( pMod );
132 }
133
GetExtraData()134 ExtraData* Dll::GetExtraData ()
135 {
136 if (!m_xExtraData)
137 m_xExtraData.reset(new ExtraData);
138 return m_xExtraData.get();
139 }
140
141 } // namespace
142
143
144 // basctl::ExtraData
145
146
ExtraData()147 ExtraData::ExtraData () :
148 bChoosingMacro(false),
149 bShellInCriticalSection(false)
150 {
151 StarBASIC::SetGlobalBreakHdl(LINK(this, ExtraData, GlobalBasicBreakHdl));
152 }
153
~ExtraData()154 ExtraData::~ExtraData ()
155 {
156 // Resetting ErrorHdl is cleaner indeed but this instance is destroyed
157 // pretty late, after the last Basic, anyway.
158 // Due to the call there is AppData created then though and not
159 // destroyed anymore => MLK's at Purify
160 // StarBASIC::SetGlobalErrorHdl( Link() );
161 // StarBASIC::SetGlobalBreakHdl( Link() );
162 // StarBASIC::setGlobalStarScriptListener( XEngineListenerRef() );
163 }
164
IMPL_STATIC_LINK(ExtraData,GlobalBasicBreakHdl,StarBASIC *,pBasic,BasicDebugFlags)165 IMPL_STATIC_LINK(ExtraData, GlobalBasicBreakHdl, StarBASIC *, pBasic, BasicDebugFlags)
166 {
167 BasicDebugFlags nRet = BasicDebugFlags::NONE;
168 if (Shell* pShell = GetShell())
169 {
170 if (BasicManager* pBasMgr = FindBasicManager(pBasic))
171 {
172 // I do get here twice if Step into protected Basic
173 // => bad, if password query twice, also you don't see
174 // the lib in the PasswordDlg...
175 // => start no password query at this point
176 ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
177 OSL_ENSURE( aDocument.isValid(), "basctl::ExtraData::GlobalBasicBreakHdl: no document for the basic manager!" );
178 if ( aDocument.isValid() )
179 {
180 OUString aOULibName( pBasic->GetName() );
181 Reference< script::XLibraryContainer > xModLibContainer = aDocument.getLibraryContainer( E_SCRIPTS );
182 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
183 {
184 Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
185 if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) )
186 {
187 // a step-out should get me out of the protected area...
188 nRet = BasicDebugFlags::StepOut;
189 }
190 else
191 {
192 nRet = pShell->CallBasicBreakHdl( pBasic );
193 }
194 }
195 }
196 }
197 }
198
199 return nRet;
200 }
201
202
203 } // namespace basctl
204
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
206