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 <com/sun/star/i18n/TextConversionOption.hpp>
21 
22 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
23 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
24 #include <com/sun/star/lang/XInitialization.hpp>
25 #include <com/sun/star/awt/XWindow.hpp>
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <comphelper/propertysequence.hxx>
28 #include <cppuhelper/bootstrap.hxx>
29 #include <svl/style.hxx>
30 #include <editeng/eeitem.hxx>
31 #include <editeng/langitem.hxx>
32 #include <editeng/fontitem.hxx>
33 
34 #include <fuhhconv.hxx>
35 #include <drawdoc.hxx>
36 #include <Outliner.hxx>
37 #include <DrawViewShell.hxx>
38 #include <OutlineViewShell.hxx>
39 #include <Window.hxx>
40 #include <ViewShellBase.hxx>
41 
42 #include <sdresid.hxx>
43 #include <strings.hrc>
44 
45 class SfxRequest;
46 
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::beans;
49 using namespace ::com::sun::star::uno;
50 
51 namespace sd {
52 
53 
FuHangulHanjaConversion(ViewShell * pViewSh,::sd::Window * pWin,::sd::View * pView,SdDrawDocument * pDocument,SfxRequest & rReq)54 FuHangulHanjaConversion::FuHangulHanjaConversion (
55     ViewShell* pViewSh,
56     ::sd::Window* pWin,
57     ::sd::View* pView,
58     SdDrawDocument* pDocument,
59     SfxRequest& rReq )
60        : FuPoor(pViewSh, pWin, pView, pDocument, rReq),
61     pSdOutliner(nullptr),
62     bOwnOutliner(false)
63 {
64     if ( dynamic_cast< const DrawViewShell *>( mpViewShell ) !=  nullptr )
65     {
66         bOwnOutliner = true;
67         pSdOutliner = new SdOutliner( mpDoc, OutlinerMode::TextObject );
68     }
69     else if ( dynamic_cast< const OutlineViewShell *>( mpViewShell ) !=  nullptr )
70     {
71         bOwnOutliner = false;
72         pSdOutliner = mpDoc->GetOutliner();
73     }
74 
75     if (pSdOutliner)
76        pSdOutliner->PrepareSpelling();
77 }
78 
~FuHangulHanjaConversion()79 FuHangulHanjaConversion::~FuHangulHanjaConversion()
80 {
81     if (pSdOutliner)
82         pSdOutliner->EndConversion();
83 
84     if (bOwnOutliner)
85         delete pSdOutliner;
86 }
87 
Create(ViewShell * pViewSh,::sd::Window * pWin,::sd::View * pView,SdDrawDocument * pDoc,SfxRequest & rReq)88 rtl::Reference<FuPoor> FuHangulHanjaConversion::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq )
89 {
90     rtl::Reference<FuPoor> xFunc( new FuHangulHanjaConversion( pViewSh, pWin, pView, pDoc, rReq ) );
91     return xFunc;
92 }
93 
94 /**
95  * Search and replace
96  */
StartConversion(LanguageType nSourceLanguage,LanguageType nTargetLanguage,const vcl::Font * pTargetFont,sal_Int32 nOptions,bool bIsInteractive)97 void FuHangulHanjaConversion::StartConversion( LanguageType nSourceLanguage, LanguageType nTargetLanguage,
98         const vcl::Font *pTargetFont, sal_Int32 nOptions, bool bIsInteractive )
99 {
100 
101     mpView->BegUndo(SdResId(STR_UNDO_HANGULHANJACONVERSION));
102 
103     ViewShellBase* pBase = dynamic_cast<ViewShellBase*>( SfxViewShell::Current() );
104     if (pBase != nullptr)
105         mpViewShell = pBase->GetMainViewShell().get();
106 
107     if( mpViewShell )
108     {
109         if ( pSdOutliner && dynamic_cast< const DrawViewShell *>( mpViewShell ) !=  nullptr && !bOwnOutliner )
110         {
111             pSdOutliner->EndConversion();
112 
113             bOwnOutliner = true;
114             pSdOutliner = new SdOutliner( mpDoc, OutlinerMode::TextObject );
115             pSdOutliner->BeginConversion();
116         }
117         else if ( pSdOutliner && dynamic_cast< const OutlineViewShell *>( mpViewShell ) !=  nullptr && bOwnOutliner )
118         {
119             pSdOutliner->EndConversion();
120             delete pSdOutliner;
121 
122             bOwnOutliner = false;
123             pSdOutliner = mpDoc->GetOutliner();
124             pSdOutliner->BeginConversion();
125         }
126 
127         if (pSdOutliner)
128             pSdOutliner->StartConversion(nSourceLanguage, nTargetLanguage, pTargetFont, nOptions, bIsInteractive );
129     }
130 
131     // Due to changing between edit mode, notes mode, and handout mode the
132     // view has most likely changed.  Get the new one.
133     mpViewShell = pBase ? pBase->GetMainViewShell().get() : nullptr;
134     if (mpViewShell != nullptr)
135     {
136         mpView = mpViewShell->GetView();
137         mpWindow = mpViewShell->GetActiveWindow();
138     }
139     else
140     {
141         mpView = nullptr;
142         mpWindow = nullptr;
143     }
144 
145     if (mpView != nullptr)
146         mpView->EndUndo();
147 }
148 
ConvertStyles(LanguageType nTargetLanguage,const vcl::Font * pTargetFont)149 void FuHangulHanjaConversion::ConvertStyles( LanguageType nTargetLanguage, const vcl::Font *pTargetFont )
150 {
151     if( !mpDoc )
152         return;
153 
154     SfxStyleSheetBasePool* pStyleSheetPool = mpDoc->GetStyleSheetPool();
155     if( !pStyleSheetPool )
156         return;
157 
158     SfxStyleSheetBase* pStyle = pStyleSheetPool->First(SfxStyleFamily::All);
159     while( pStyle )
160     {
161         SfxItemSet& rSet = pStyle->GetItemSet();
162 
163         const bool bHasParent = !pStyle->GetParent().isEmpty();
164 
165         if( !bHasParent || rSet.GetItemState( EE_CHAR_LANGUAGE_CJK, false ) == SfxItemState::SET )
166             rSet.Put( SvxLanguageItem( nTargetLanguage, EE_CHAR_LANGUAGE_CJK ) );
167 
168         if( pTargetFont &&
169             ( !bHasParent || rSet.GetItemState( EE_CHAR_FONTINFO_CJK, false ) == SfxItemState::SET ) )
170         {
171             // set new font attribute
172             SvxFontItem aFontItem( rSet.Get( EE_CHAR_FONTINFO_CJK ) );
173             aFontItem.SetFamilyName(   pTargetFont->GetFamilyName());
174             aFontItem.SetFamily(       pTargetFont->GetFamilyType());
175             aFontItem.SetStyleName(    pTargetFont->GetStyleName());
176             aFontItem.SetPitch(        pTargetFont->GetPitch());
177             aFontItem.SetCharSet(      pTargetFont->GetCharSet());
178             rSet.Put( aFontItem );
179         }
180 
181         pStyle = pStyleSheetPool->Next();
182     }
183 
184     mpDoc->SetLanguage( nTargetLanguage, EE_CHAR_LANGUAGE_CJK );
185 }
186 
StartChineseConversion()187 void FuHangulHanjaConversion::StartChineseConversion()
188 {
189     //open ChineseTranslationDialog
190     Reference< XComponentContext > xContext(
191         ::cppu::defaultBootstrap_InitialComponentContext() ); //@todo get context from calc if that has one
192     if(!xContext.is())
193         return;
194 
195     Reference< lang::XMultiComponentFactory > xMCF( xContext->getServiceManager() );
196     if(!xMCF.is())
197         return;
198 
199     Reference< ui::dialogs::XExecutableDialog > xDialog(
200             xMCF->createInstanceWithContext("com.sun.star.linguistic2.ChineseTranslationDialog"
201                 , xContext), UNO_QUERY);
202     Reference< lang::XInitialization > xInit( xDialog, UNO_QUERY );
203     if( xInit.is() )
204     {
205         //  initialize dialog
206         Reference< awt::XWindow > xDialogParentWindow;
207         Sequence<Any> aSeq(comphelper::InitAnyPropertySequence(
208         {
209             {"ParentWindow", uno::Any(xDialogParentWindow)}
210         }));
211         xInit->initialize( aSeq );
212 
213         //execute dialog
214         sal_Int16 nDialogRet = xDialog->execute();
215         if( RET_OK == nDialogRet )
216         {
217             //get some parameters from the dialog
218             bool bToSimplified = true;
219             bool bUseVariants = true;
220             bool bCommonTerms = true;
221             Reference< beans::XPropertySet >  xProp( xDialog, UNO_QUERY );
222             if( xProp.is() )
223             {
224                 try
225                 {
226                     xProp->getPropertyValue( "IsDirectionToSimplified" ) >>= bToSimplified;
227                     xProp->getPropertyValue( "IsUseCharacterVariants" ) >>= bUseVariants;
228                     xProp->getPropertyValue( "IsTranslateCommonTerms" ) >>= bCommonTerms;
229                 }
230                 catch( Exception& )
231                 {
232                 }
233             }
234 
235             //execute translation
236             LanguageType nSourceLang = bToSimplified ? LANGUAGE_CHINESE_TRADITIONAL : LANGUAGE_CHINESE_SIMPLIFIED;
237             LanguageType nTargetLang = bToSimplified ? LANGUAGE_CHINESE_SIMPLIFIED : LANGUAGE_CHINESE_TRADITIONAL;
238             sal_Int32 nOptions       = bUseVariants ? i18n::TextConversionOption::USE_CHARACTER_VARIANTS : 0;
239             if( !bCommonTerms )
240                 nOptions = nOptions | i18n::TextConversionOption::CHARACTER_BY_CHARACTER;
241 
242             vcl::Font aTargetFont = OutputDevice::GetDefaultFont(
243                                 DefaultFontType::CJK_PRESENTATION,
244                                 nTargetLang, GetDefaultFontFlags::OnlyOne );
245 
246             StartConversion( nSourceLang, nTargetLang, &aTargetFont, nOptions, false );
247             ConvertStyles( nTargetLang, &aTargetFont );
248         }
249     }
250     Reference< lang::XComponent > xComponent( xDialog, UNO_QUERY );
251     if( xComponent.is() )
252         xComponent->dispose();
253 }
254 } // end of namespace
255 
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
257