<commit_before>/* -*- 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 <helper/titlebarupdate.hxx>

#include <pattern/window.hxx>
#include <threadhelp/writeguard.hxx>
#include <threadhelp/readguard.hxx>
#include <macros/generic.hxx>
#include <services.h>
#include <properties.h>

#include <com/sun/star/awt/XWindow.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <com/sun/star/frame/ModuleManager.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/XMaterialHolder.hpp>
#include <com/sun/star/frame/XTitleChangeBroadcaster.hpp>
#include <com/sun/star/beans/NamedValue.hpp>

#include <comphelper/processfactory.hxx>
#include <comphelper/sequenceashashmap.hxx>
#include <unotools/configmgr.hxx>
#include <unotools/bootstrap.hxx>
#include <vcl/window.hxx>
#include <vcl/syswin.hxx>
#include <toolkit/helper/vclunohelper.hxx>
#include <vcl/svapp.hxx>
#include <vcl/wrkwin.hxx>
#include <tools/diagnose_ex.h>

namespace framework{

static const ::sal_Int32 INVALID_ICON_ID = -1;
static const ::sal_Int32 DEFAULT_ICON_ID =  0;


//*****************************************************************************************************************
TitleBarUpdate::TitleBarUpdate(const css::uno::Reference< css::uno::XComponentContext >& xContext)
    : ThreadHelpBase          (&Application::GetSolarMutex())
    , m_xContext              (xContext                     )
    , m_xFrame                (                             )
{
}

//*****************************************************************************************************************
TitleBarUpdate::~TitleBarUpdate()
{
}

//*****************************************************************************************************************
void SAL_CALL TitleBarUpdate::initialize(const css::uno::Sequence< css::uno::Any >& lArguments)
    throw(css::uno::Exception       ,
          css::uno::RuntimeException)
{
    // check arguments
    css::uno::Reference< css::frame::XFrame > xFrame;
    if (lArguments.getLength() < 1)
        throw css::lang::IllegalArgumentException(
                "Empty argument list!",
                static_cast< ::cppu::OWeakObject* >(this),
                1);

    lArguments[0] >>= xFrame;
    if (!xFrame.is())
        throw css::lang::IllegalArgumentException(
                "No valid frame specified!",
                static_cast< ::cppu::OWeakObject* >(this),
                1);

    // SYNCHRONIZED ->
    WriteGuard aWriteLock(m_aLock);
    // hold the frame as weak reference(!) so it can die everytimes :-)
    m_xFrame = xFrame;
    aWriteLock.unlock();
    // <- SYNCHRONIZED

    // start listening
    xFrame->addFrameActionListener(this);

    css::uno::Reference< css::frame::XTitleChangeBroadcaster > xBroadcaster(xFrame, css::uno::UNO_QUERY);
    if (xBroadcaster.is ())
        xBroadcaster->addTitleChangeListener (this);
}

//*****************************************************************************************************************
void SAL_CALL TitleBarUpdate::frameAction(const css::frame::FrameActionEvent& aEvent)
    throw(css::uno::RuntimeException)
{
    // we are interested on events only, which must trigger a title bar update
    // because component was changed.
    if (
        (aEvent.Action == css::frame::FrameAction_COMPONENT_ATTACHED  ) ||
        (aEvent.Action == css::frame::FrameAction_COMPONENT_REATTACHED) ||
        (aEvent.Action == css::frame::FrameAction_COMPONENT_DETACHING )
       )
    {
        impl_forceUpdate ();
    }
}

//*****************************************************************************************************************
void SAL_CALL TitleBarUpdate::titleChanged(const css::frame::TitleChangedEvent& /* aEvent */)
    throw (css::uno::RuntimeException)
{
    impl_forceUpdate ();
}

//*****************************************************************************************************************
void SAL_CALL TitleBarUpdate::disposing(const css::lang::EventObject&)
    throw(css::uno::RuntimeException)
{
    // nothing todo here - because we hold the frame as weak reference only
}

//http://live.gnome.org/GnomeShell/ApplicationBased
//See http://msdn.microsoft.com/en-us/library/dd378459(v=VS.85).aspx for future
//Windows 7 equivalent support
void TitleBarUpdate::impl_updateApplicationID(const css::uno::Reference< css::frame::XFrame >& xFrame)
{
    css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow ();
    if ( ! xWindow.is() )
        return;

    OUString sApplicationID;
    try
    {
        // SYNCHRONIZED ->
        ReadGuard aReadLock(m_aLock);
        css::uno::Reference< css::uno::XComponentContext > xContext = m_xContext;
        aReadLock.unlock();
        // <- SYNCHRONIZED

        css::uno::Reference< css::frame::XModuleManager2 > xModuleManager =
            css::frame::ModuleManager::create( xContext );

#if !defined(MACOSX)
        OUString sDesktopName;
        OUString aModuleId = xModuleManager->identify(xFrame);
        if ( aModuleId == "com.sun.star.text.TextDocument" ||
             aModuleId == "com.sun.star.text.GlobalDocument" ||
             aModuleId == "com.sun.star.text.WebDocument" ||
             aModuleId == "com.sun.star.xforms.XMLFormDocument" )
            sDesktopName = "Writer";
        else if ( aModuleId == "com.sun.star.sheet.SpreadsheetDocument" )
            sDesktopName = "Calc";
        else if ( aModuleId == "com.sun.star.presentation.PresentationDocument" )
            sDesktopName = "Impress";
        else if ( aModuleId == "com.sun.star.drawing.DrawingDocument" )
            sDesktopName = "Draw";
        else if ( aModuleId == "com.sun.star.formula.FormulaProperties" )
            sDesktopName = "Math";
        else if ( aModuleId == "com.sun.star.sdb.DatabaseDocument" ||
                  aModuleId == "com.sun.star.sdb.OfficeDatabaseDocument" ||
                  aModuleId == "com.sun.star.sdb.RelationDesign" ||
                  aModuleId == "com.sun.star.sdb.QueryDesign" ||
                  aModuleId == "com.sun.star.sdb.TableDesign" ||
                  aModuleId == "com.sun.star.sdb.DataSourceBrowser" )
            sDesktopName = "Base";
        else
            sDesktopName = "Startcenter";
#if defined(WNT)
        // We use a hardcoded product name matching the registry keys so applications can be associated with file types
        sApplicationID = "TheDocumentFoundation.LibreOffice.";
        sApplicationID += sDesktopName;
#else
        sApplicationID = utl::ConfigManager::getProductName().toAsciiLowerCase();
        sApplicationID += "-";
        sApplicationID += sDesktopName.toAsciiLowerCase();
#endif
#endif
    }
    catch(const css::uno::Exception&)
    {
    }

    // VCL SYNCHRONIZED ->
    SolarMutexGuard aSolarGuard;

    Window* pWindow = (VCLUnoHelper::GetWindow( xWindow ));
    if (<commit_msg>The #ifndef MACOSX can surround even more code<commit_after>/* -*- 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 <helper/titlebarupdate.hxx>

#include <pattern/window.hxx>
#include <threadhelp/writeguard.hxx>
#include <threadhelp/readguard.hxx>
#include <macros/generic.hxx>
#include <services.h>
#include <properties.h>

#include <com/sun/star/awt/XWindow.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <com/sun/star/frame/ModuleManager.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/XMaterialHolder.hpp>
#include <com/sun/star/frame/XTitleChangeBroadcaster.hpp>
#include <com/sun/star/beans/NamedValue.hpp>

#include <comphelper/processfactory.hxx>
#include <comphelper/sequenceashashmap.hxx>
#include <unotools/configmgr.hxx>
#include <unotools/bootstrap.hxx>
#include <vcl/window.hxx>
#include <vcl/syswin.hxx>
#include <toolkit/helper/vclunohelper.hxx>
#include <vcl/svapp.hxx>
#include <vcl/wrkwin.hxx>
#include <tools/diagnose_ex.h>

namespace framework{

static const ::sal_Int32 INVALID_ICON_ID = -1;
static const ::sal_Int32 DEFAULT_ICON_ID =  0;


//*****************************************************************************************************************
TitleBarUpdate::TitleBarUpdate(const css::uno::Reference< css::uno::XComponentContext >& xContext)
    : ThreadHelpBase          (&Application::GetSolarMutex())
    , m_xContext              (xContext                     )
    , m_xFrame                (                             )
{
}

//*****************************************************************************************************************
TitleBarUpdate::~TitleBarUpdate()
{
}

//*****************************************************************************************************************
void SAL_CALL TitleBarUpdate::initialize(const css::uno::Sequence< css::uno::Any >& lArguments)
    throw(css::uno::Exception       ,
          css::uno::RuntimeException)
{
    // check arguments
    css::uno::Reference< css::frame::XFrame > xFrame;
    if (lArguments.getLength() < 1)
        throw css::lang::IllegalArgumentException(
                "Empty argument list!",
                static_cast< ::cppu::OWeakObject* >(this),
                1);

    lArguments[0] >>= xFrame;
    if (!xFrame.is())
        throw css::lang::IllegalArgumentException(
                "No valid frame specified!",
                static_cast< ::cppu::OWeakObject* >(this),
                1);

    // SYNCHRONIZED ->
    WriteGuard aWriteLock(m_aLock);
    // hold the frame as weak reference(!) so it can die everytimes :-)
    m_xFrame = xFrame;
    aWriteLock.unlock();
    // <- SYNCHRONIZED

    // start listening
    xFrame->addFrameActionListener(this);

    css::uno::Reference< css::frame::XTitleChangeBroadcaster > xBroadcaster(xFrame, css::uno::UNO_QUERY);
    if (xBroadcaster.is ())
        xBroadcaster->addTitleChangeListener (this);
}

//*****************************************************************************************************************
void SAL_CALL TitleBarUpdate::frameAction(const css::frame::FrameActionEvent& aEvent)
    throw(css::uno::RuntimeException)
{
    // we are interested on events only, which must trigger a title bar update
    // because component was changed.
    if (
        (aEvent.Action == css::frame::FrameAction_COMPONENT_ATTACHED  ) ||
        (aEvent.Action == css::frame::FrameAction_COMPONENT_REATTACHED) ||
        (aEvent.Action == css::frame::FrameAction_COMPONENT_DETACHING )
       )
    {
        impl_forceUpdate ();
    }
}

//*****************************************************************************************************************
void SAL_CALL TitleBarUpdate::titleChanged(const css::frame::TitleChangedEvent& /* aEvent */)
    throw (css::uno::RuntimeException)
{
    impl_forceUpdate ();
}

//*****************************************************************************************************************
void SAL_CALL TitleBarUpdate::disposing(const css::lang::EventObject&)
    throw(css::uno::RuntimeException)
{
    // nothing todo here - because we hold the frame as weak reference only
}

//http://live.gnome.org/GnomeShell/ApplicationBased
//See http://msdn.microsoft.com/en-us/library/dd378459(v=VS.85).aspx for future
//Windows 7 equivalent support
void TitleBarUpdate::impl_updateApplicationID(const css::uno::Reference< css::frame::XFrame >& xFrame)
{
    css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow ();
    if ( ! xWindow.is() )
        return;

    OUString sApplicationID;

#if !defined(MACOSX)
    try
    {
        // SYNCHRONIZED ->
        ReadGuard aReadLock(m_aLock);
        css::uno::Reference< css::uno::XComponentContext > xContext = m_xContext;
        aReadLock.unlock();
        // <- SYNCHRONIZED

        css::uno::Reference< css::frame::XModuleManager2 > xModuleManager =
            css::frame::ModuleManager::create( xContext );

        OUString sDesktopName;
        OUString aModuleId = xModuleManager->identify(xFrame);
        if ( aModuleId == "com.sun.star.text.TextDocument" ||
             aModuleId == "com.sun.star.text.GlobalDocument" ||
             aModuleId == "com.sun.star.text.WebDocument" ||
             aModuleId == "com.sun.star.xforms.XMLFormDocument" )
            sDesktopName = "Writer";
        else if ( aModuleId == "com.sun.star.sheet.SpreadsheetDocument" )
            sDesktopName = "Calc";
        else if ( aModuleId == "com.sun.star.presentation.PresentationDocument" )
            sDesktopName = "Impress";
        else if ( aModuleId == "com.sun.star.drawing.DrawingDocument" )
            sDesktopName = "Draw";
        else if ( aModuleId == "com.sun.star.formula.FormulaProperties" )
            sDesktopName = "Math";
        else if ( aModuleId == "com.sun.star.sdb.DatabaseDocument" ||
                  aModuleId == "com.sun.star.sdb.OfficeDatabaseDocument" ||
                  aModuleId == "com.sun.star.sdb.RelationDesign" ||
                  aModuleId == "com.sun.star.sdb.QueryDesign" ||
                  aModuleId == "com.sun.star.sdb.TableDesign" ||
                  aModuleId == "com.sun.star.sdb.DataSourceBrowser" )
            sDesktopName = "Base";
        else
            sDesktopName = "Startcenter";
#if defined(WNT)
        // We use a hardcoded product name matching the registry keys so applications can be associated with file types
        sApplicationID = "TheDocumentFoundation.LibreOffice.";
        sApplicationID += sDesktopName;
#else
        sApplicationID = utl::ConfigManager::getProductName().toAsciiLowerCase();
        sApplicationID += "-";
        sApplicationID += sDesktopName.toAsciiLowerCase();
#endif
    }
    catch(const css::uno::Exception&)
    {
    }
#endif

    // VCL SYNCHRONIZED ->
    SolarMutexGuard aSolarGuard;

    Window* pWindow = (VCLUnoHelper::GetWindow( xWindow ));
    if (
{'fraction_non_alphanumeric': 0.20418948172672863, 'fraction_numerical': 0.0038201961033999744, 'mean_word_length': 3.4357526122564246, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7546601', 'n_tokens_mistral': 4648, 'n_tokens_neox': 4447, 'n_words': 939}