/**************************************************************
 * 
 * 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
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * 
 *************************************************************/



// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_cui.hxx"

// include ---------------------------------------------------------------

#define _SVX_OPTIMPROVE_CXX

#include <optimprove.hxx>
#include <dialmgr.hxx>
#include <vcl/msgbox.hxx>

#include "optimprove.hrc"
#include "helpid.hrc"
#include <cuires.hrc>
#include <svx/dialogs.hrc>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/oooimprovement/XCore.hpp>
#include <com/sun/star/oooimprovement/XCoreController.hpp>
#include <com/sun/star/system/XSystemShellExecute.hpp>
#include <com/sun/star/system/SystemShellExecuteFlags.hpp>
#include <com/sun/star/util/XStringSubstitution.hpp>
#include <comphelper/configurationhelper.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/synchronousdispatch.hxx>
#include <comphelper/uieventslogger.hxx>
#include <tools/testtoolloader.hxx>
#include <osl/file.hxx>

#define C2S(s)  ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(s))

namespace beans  = ::com::sun::star::beans;
namespace lang  = ::com::sun::star::lang;
namespace uno   = ::com::sun::star::uno;
namespace util  = ::com::sun::star::util;
using namespace com::sun::star::system;


namespace
{
    bool lcl_doesLogfileExist(const ::rtl::OUString& sLogPath)
    {
        ::rtl::OUString sLogFile( sLogPath );
        sLogFile += C2S("/Current.csv");
        ::osl::File aLogFile(sLogFile);
        return aLogFile.open(osl_File_OpenFlag_Read) == ::osl::FileBase::E_None;
    }
}

// class SvxImprovementOptionsPage ---------------------------------------

SvxImprovementOptionsPage::SvxImprovementOptionsPage( Window* pParent, const SfxItemSet& rSet ) :

    SfxTabPage( pParent, CUI_RES( RID_SVXPAGE_IMPROVEMENT ), rSet ),

    m_aImproveFL                ( this, CUI_RES( FL_IMPROVE ) ),
    m_aInvitationFT             ( this, CUI_RES( FT_INVITATION ) ),
    m_aYesRB                    ( this, CUI_RES( RB_YES ) ),
    m_aNoRB                     ( this, CUI_RES( RB_NO ) ),
    m_aInfoFI                   ( this, CUI_RES( FI_INFO ) ),
    m_aDataFL                   ( this, CUI_RES( FL_DATA ) ),
    m_aNumberOfReportsFT        ( this, CUI_RES( FT_NR_REPORTS ) ),
    m_aNumberOfReportsValueFT   ( this, CUI_RES( FT_NR_REPORTS_VALUE ) ),
    m_aNumberOfActionsFT        ( this, CUI_RES( FT_NR_ACTIONS ) ),
    m_aNumberOfActionsValueFT   ( this, CUI_RES( FT_NR_ACTIONS_VALUE ) ),
    m_aShowDataPB               ( this, CUI_RES( PB_SHOWDATA ) ),

    m_sInfo                     (       CUI_RES( STR_INFO ) ),
    m_sMoreInfo                 (       CUI_RES( STR_MOREINFO ) )

{
    FreeResource();

    m_aInfoFI.SetURL( C2S( "www.sun.com/privacy/" ) );
    m_aInfoFI.SetClickHdl( LINK( this, SvxImprovementOptionsPage, HandleHyperlink ) );
    m_aShowDataPB.SetClickHdl( LINK( this, SvxImprovementOptionsPage, HandleShowData ) );
}

SvxImprovementOptionsPage::~SvxImprovementOptionsPage()
{
}

IMPL_LINK( SvxImprovementOptionsPage, HandleHyperlink, svt::FixedHyperlinkImage*, EMPTYARG )
{
    ::rtl::OUString sURL( m_aInfoFI.GetURL() );

    if ( sURL.getLength() > 0 )
    {
        try
        {
            uno::Reference< lang::XMultiServiceFactory > xSMGR =
                ::comphelper::getProcessServiceFactory();
            uno::Reference< XSystemShellExecute > xSystemShell(
                xSMGR->createInstance( ::rtl::OUString(
                    RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.system.SystemShellExecute" ) ) ),
                uno::UNO_QUERY_THROW );
            if ( xSystemShell.is() )
            {
                xSystemShell->execute(
                    sURL, ::rtl::OUString(), SystemShellExecuteFlags::DEFAULTS );
            }
        }
        catch( const uno::Exception& e )
        {
             OSL_TRACE( "Caught exception: %s\n thread terminated.\n",
                rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
        }
    }

    return 0;
}

IMPL_LINK( SvxImprovementOptionsPage, HandleShowData, PushButton*, EMPTYARG )
{
    uno::Reference < uno::XInterface > xDesktop( ::comphelper::getProcessServiceFactory()->createInstance(
        C2S("com.sun.star.frame.Desktop") ), uno::UNO_QUERY );
    if ( xDesktop.is() )
    {
        ::rtl::OUString sLogFile( m_sLogPath );
        sLogFile += C2S("/Current.csv");
        uno::Sequence< beans::PropertyValue > aArgs(3);
		aArgs[0].Name = ::rtl::OUString::createFromAscii("FilterName");
		aArgs[0].Value = uno::makeAny(::rtl::OUString::createFromAscii("Text - txt - csv (StarCalc)"));
		aArgs[1].Name = ::rtl::OUString::createFromAscii("FilterOptions");
		aArgs[1].Value = uno::makeAny(::rtl::OUString::createFromAscii("44,34,12,1,"));
		aArgs[2].Name = ::rtl::OUString::createFromAscii("ReadOnly");
		aArgs[2].Value = uno::makeAny(true);

        uno::Reference< lang::XComponent > xDoc = ::comphelper::SynchronousDispatch::dispatch(
            xDesktop, sLogFile, C2S("_default"), 0, aArgs );
        if ( xDoc.is() )
        {
            dynamic_cast<Dialog*>(GetParent())->EndDialog( RET_CANCEL );
            return 1;
        }
    }

    return 0;
}

SfxTabPage* SvxImprovementOptionsPage::Create( Window* pParent, const SfxItemSet& rSet )
{
    return new SvxImprovementOptionsPage( pParent, rSet );
}

sal_Bool SvxImprovementOptionsPage::FillItemSet( SfxItemSet& /*rSet*/ )
{
    uno::Reference< lang::XMultiServiceFactory > xSMGR = ::comphelper::getProcessServiceFactory();
    uno::Reference< uno::XInterface > xConfig;

    try
    {
        xConfig = ::comphelper::ConfigurationHelper::openConfig(
            xSMGR, C2S("/org.openoffice.Office.OOoImprovement.Settings"),
            ::comphelper::ConfigurationHelper::E_STANDARD );
        ::comphelper::ConfigurationHelper::writeRelativeKey(
            xConfig, C2S("Participation"), C2S("ShowedInvitation"), uno::makeAny( true ) );
        ::comphelper::ConfigurationHelper::writeRelativeKey(
            xConfig, C2S("Participation"), C2S("InvitationAccepted"), uno::makeAny( m_aYesRB.IsChecked() != sal_False ) );
        ::comphelper::ConfigurationHelper::flush( xConfig );
        // TODO: refactor
        ::comphelper::UiEventsLogger::reinit();
        ::tools::InitTestToolLib();
    }
    catch( uno::Exception& )
    {
    }

    return sal_False;
}

void SvxImprovementOptionsPage::Reset( const SfxItemSet& /*rSet*/ )
{
    uno::Reference< lang::XMultiServiceFactory > xSMGR = ::comphelper::getProcessServiceFactory();
    uno::Reference< com::sun::star::oooimprovement::XCore > xCore(
        xSMGR->createInstance( C2S("com.sun.star.oooimprovement.Core") ),
        uno::UNO_QUERY );
    uno::Reference< uno::XInterface > xConfig;

    try
    {
        m_aNoRB.Check();
        xConfig = ::comphelper::ConfigurationHelper::openConfig(
            xSMGR, C2S("/org.openoffice.Office.OOoImprovement.Settings"),
            ::comphelper::ConfigurationHelper::E_READONLY );
        if ( xConfig.is() )
        {
            bool bYesChecked = false;
            uno::Any aAny = ::comphelper::ConfigurationHelper::
                readRelativeKey( xConfig, C2S("Participation"), C2S("ShowedInvitation") );
            if ( ( aAny >>= bYesChecked ) && bYesChecked )
            {
                bool bTemp = false;
                aAny = ::comphelper::ConfigurationHelper::
                    readRelativeKey( xConfig, C2S("Participation"), C2S("InvitationAccepted") );
                if ( aAny >>= bTemp )
                {
                    bYesChecked &= bTemp;
                    if ( bYesChecked )
                        m_aYesRB.Check();
                }
            }

            ::rtl::OUString sURL;
            aAny = ::comphelper::ConfigurationHelper::
                readRelativeKey( xConfig, C2S("Participation"), C2S("HelpUrl") );
            if ( aAny >>= sURL )
                m_aInfoFI.SetURL( sURL );

            sal_Int32 nCount = 0;
            aAny = ::comphelper::ConfigurationHelper::
                readRelativeKey( xConfig, C2S("Counters"), C2S("UploadedReports") );
            if ( aAny >>= nCount )
                m_aNumberOfReportsValueFT.SetText( String::CreateFromInt32( nCount ) );
            aAny = ::comphelper::ConfigurationHelper::
                readRelativeKey( xConfig, C2S("Counters"), C2S("LoggedEvents") );
            if ( aAny >>= nCount )
            {
                if ( xCore.is() )
                    nCount += xCore->getSessionLogEventCount();
                m_aNumberOfActionsValueFT.SetText( String::CreateFromInt32( nCount ) );
            }

            ::rtl::OUString sPath;
            aAny = ::comphelper::ConfigurationHelper::readDirectKey(
                xSMGR, C2S("/org.openoffice.Office.Logging"), C2S("OOoImprovement"),
                C2S("LogPath"), ::comphelper::ConfigurationHelper::E_READONLY );
            if ( aAny >>= sPath )
            {
                uno::Reference< util::XStringSubstitution > xSubst(
                    xSMGR->createInstance( C2S("com.sun.star.util.PathSubstitution") ),
                    uno::UNO_QUERY );
                if ( xSubst.is() )
                    sPath = xSubst->substituteVariables( sPath, sal_False );
                m_sLogPath = sPath;
                m_aShowDataPB.Enable(lcl_doesLogfileExist(m_sLogPath));
            }
        }
    }
    catch( uno::Exception& )
    {
        m_aShowDataPB.Enable(false);
    }
}

