Kaydet (Commit) 49696cc7 authored tarafından Mike Kaganski's avatar Mike Kaganski

Some refactor of lockfile classes to minimize interface

Change-Id: Icc67c31d6a2351b6504429e25067c25353233f5f
Reviewed-on: https://gerrit.libreoffice.org/69947
Tested-by: Jenkins
Reviewed-by: 's avatarMike Kaganski <mike.kaganski@collabora.com>
üst 107fd827
......@@ -33,10 +33,7 @@ namespace svt {
class SVL_DLLPUBLIC GenDocumentLockFile : public LockFileCommon
{
public:
/// Specify the lockfile URL directly
GenDocumentLockFile( const OUString& aURL );
/// Let the object generate and own URL based on the original file's URL and a prefix
GenDocumentLockFile( const OUString& aOrigURL, const OUString& aPrefix );
GenDocumentLockFile(const OUString& aLockFileURL);
virtual ~GenDocumentLockFile() override;
bool CreateOwnLockFile();
......
......@@ -47,19 +47,16 @@ private:
protected:
::osl::Mutex m_aMutex;
/// This method generates the URL of the lock file based on the document URL and the specified prefix.
static OUString GenerateOwnLockFileURL(const OUString& aOrigURL, const OUString& aPrefix);
public:
/// Specify the lockfile URL directly
LockFileCommon( const OUString& aURL );
/// Let the object generate and own URL based on the original file's URL and a prefix
LockFileCommon( const OUString& aOrigURL, const OUString& aPrefix );
LockFileCommon(const OUString& aLockFileURL);
virtual ~LockFileCommon();
const OUString& GetURL() const;
void SetURL(const OUString& aURL);
/// This method generates the URL of the lock file based on the document URL and the specified prefix.
virtual OUString GenerateURL( const OUString& aOrigURL, const OUString& aPrefix );
static void ParseList( const css::uno::Sequence< sal_Int8 >& aBuffer, std::vector< LockFileEntry > &rOutput );
static LockFileEntry ParseEntry( const css::uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& o_nCurPos );
static OUString ParseName( const css::uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& o_nCurPos );
......
......@@ -11,38 +11,10 @@
#define INCLUDED_SVL_MSODOCUMENTLOCKFILE_HXX
#include <svl/svldllapi.h>
#include <svl/lockfilecommon.hxx>
#include <svl/documentlockfile.hxx>
#include <com/sun/star/lang/XComponent.hpp>
namespace com
{
namespace sun
{
namespace star
{
namespace io
{
class XInputStream;
}
}
}
}
namespace com
{
namespace sun
{
namespace star
{
namespace io
{
class XOutputStream;
}
}
}
}
#define MSO_WORD_LOCKFILE_SIZE 162
#define MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE 165
#define MSO_USERNAME_MAX_LENGTH 52
......@@ -53,11 +25,14 @@ namespace svt
class SVL_DLLPUBLIC MSODocumentLockFile : public GenDocumentLockFile
{
private:
OUString m_sOrigURL;
static bool isWordFormat(const OUString& aOrigURL);
static bool isExcelFormat(const OUString& aOrigURL);
static bool isPowerPointFormat(const OUString& aOrigURL);
enum class AppType
{
Word,
Excel,
PowerPoint
};
static AppType getAppType(const OUString& sOrigURL);
AppType m_eAppType;
protected:
virtual void
......@@ -70,9 +45,6 @@ public:
MSODocumentLockFile(const OUString& aOrigURL);
virtual ~MSODocumentLockFile() override;
/// Need to generate different lock file name for MSO.
virtual OUString GenerateURL(const OUString& aOrigURL, const OUString& aPrefix) override;
virtual LockFileEntry GetLockData() override;
virtual void RemoveFile() override;
......
......@@ -54,14 +54,8 @@ using namespace ::com::sun::star;
namespace svt {
GenDocumentLockFile::GenDocumentLockFile( const OUString& aURL )
: LockFileCommon( aURL )
{
}
GenDocumentLockFile::GenDocumentLockFile( const OUString& aOrigURL, const OUString& aPrefix )
: LockFileCommon( aOrigURL, aPrefix )
GenDocumentLockFile::GenDocumentLockFile(const OUString& aLockFileURL)
: LockFileCommon(aLockFileURL)
{
}
......@@ -179,7 +173,7 @@ void GenDocumentLockFile::RemoveFileDirectly()
DocumentLockFile::DocumentLockFile( const OUString& aOrigURL )
: GenDocumentLockFile( aOrigURL, ".~lock." )
: GenDocumentLockFile(GenerateOwnLockFileURL(aOrigURL, ".~lock."))
{
}
......
......@@ -54,17 +54,11 @@ using namespace ::com::sun::star;
namespace svt {
LockFileCommon::LockFileCommon( const OUString& aURL )
LockFileCommon::LockFileCommon(const OUString& aLockFileURL)
: m_aURL(aLockFileURL)
{
m_aURL = aURL;
}
LockFileCommon::LockFileCommon( const OUString& aOrigURL, const OUString& aPrefix )
{
m_aURL = GenerateURL(aOrigURL, aPrefix);
}
LockFileCommon::~LockFileCommon()
{
}
......@@ -82,15 +76,11 @@ void LockFileCommon::SetURL(const OUString& aURL)
}
OUString LockFileCommon::GenerateURL( const OUString& aOrigURL, const OUString& aPrefix )
OUString LockFileCommon::GenerateOwnLockFileURL(const OUString& aOrigURL, const OUString& aPrefix)
{
INetURLObject aDocURL = ResolveLinks( INetURLObject( aOrigURL ) );
OUString aShareURLString = aDocURL.GetPartBeforeLastName();
aShareURLString += aPrefix;
aShareURLString += aDocURL.GetName();
aShareURLString += "%23"; // '#'
return INetURLObject( aShareURLString ).GetMainURL( INetURLObject::DecodeMechanism::NONE );
INetURLObject aURL = ResolveLinks(INetURLObject(aOrigURL));
aURL.SetName(aPrefix + aURL.GetName() + "%23" /*'#'*/);
return aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE);
}
......
......@@ -8,7 +8,6 @@
*/
#include <svl/msodocumentlockfile.hxx>
#include <rtl/ustring.hxx>
#include <sal/log.hxx>
#include <algorithm>
#include <ucbhelper/content.hxx>
......@@ -21,60 +20,69 @@
namespace svt
{
bool MSODocumentLockFile::isWordFormat(const OUString& aOrigURL)
namespace
{
INetURLObject aDocURL = LockFileCommon::ResolveLinks(INetURLObject(aOrigURL));
return aDocURL.GetFileExtension().compareToIgnoreAsciiCase("DOC") == 0
|| aDocURL.GetFileExtension().compareToIgnoreAsciiCase("DOCX") == 0
|| aDocURL.GetFileExtension().compareToIgnoreAsciiCase("RTF") == 0
|| aDocURL.GetFileExtension().compareToIgnoreAsciiCase("ODT") == 0;
}
bool MSODocumentLockFile::isExcelFormat(const OUString& aOrigURL)
bool isWordFormat(const OUString& sExt)
{
INetURLObject aDocURL = LockFileCommon::ResolveLinks(INetURLObject(aOrigURL));
return //aDocURL.GetFileExtension().compareToIgnoreAsciiCase("XLS") || // MSO does not create lockfile for XLS
aDocURL.GetFileExtension().compareToIgnoreAsciiCase("XLSX") == 0
|| aDocURL.GetFileExtension().compareToIgnoreAsciiCase("ODS") == 0;
return sExt.equalsIgnoreAsciiCase("DOC") || sExt.equalsIgnoreAsciiCase("DOCX")
|| sExt.equalsIgnoreAsciiCase("RTF") || sExt.equalsIgnoreAsciiCase("ODT");
}
bool MSODocumentLockFile::isPowerPointFormat(const OUString& aOrigURL)
bool isExcelFormat(const OUString& sExt)
{
INetURLObject aDocURL = LockFileCommon::ResolveLinks(INetURLObject(aOrigURL));
return aDocURL.GetFileExtension().compareToIgnoreAsciiCase("PPTX") == 0
|| aDocURL.GetFileExtension().compareToIgnoreAsciiCase("PPT") == 0
|| aDocURL.GetFileExtension().compareToIgnoreAsciiCase("ODP") == 0;
return //sExt.equalsIgnoreAsciiCase("XLS") || // MSO does not create lockfile for XLS
sExt.equalsIgnoreAsciiCase("XLSX") || sExt.equalsIgnoreAsciiCase("ODS");
}
MSODocumentLockFile::MSODocumentLockFile(const OUString& aOrigURL)
: GenDocumentLockFile(GenerateURL(aOrigURL, "~$"))
, m_sOrigURL(aOrigURL)
bool isPowerPointFormat(const OUString& sExt)
{
return sExt.equalsIgnoreAsciiCase("PPTX") || sExt.equalsIgnoreAsciiCase("PPT")
|| sExt.equalsIgnoreAsciiCase("ODP");
}
MSODocumentLockFile::~MSODocumentLockFile() {}
OUString MSODocumentLockFile::GenerateURL(const OUString& aOrigURL, const OUString& aPrefix)
// Need to generate different lock file name for MSO.
OUString GenerateMSOLockFileURL(const OUString& aOrigURL)
{
INetURLObject aURL = LockFileCommon::ResolveLinks(INetURLObject(aOrigURL));
// For text documents MSO Word cuts some of the first characters of the file name
OUString sFileName = aURL.GetName();
if (isWordFormat(aOrigURL))
const OUString sExt = aURL.GetFileExtension();
if (isWordFormat(sExt))
{
const sal_Int32 nFileNameLength
= sFileName.getLength() - aURL.GetFileExtension().getLength() - 1;
const sal_Int32 nFileNameLength = sFileName.getLength() - sExt.getLength() - 1;
if (nFileNameLength >= 8)
sFileName = sFileName.copy(2);
else if (nFileNameLength == 7)
sFileName = sFileName.copy(1);
}
aURL.SetName(aPrefix + sFileName);
aURL.SetName("~$" + sFileName);
return aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE);
}
}
// static
MSODocumentLockFile::AppType MSODocumentLockFile::getAppType(const OUString& sOrigURL)
{
AppType eResult = AppType::PowerPoint;
INetURLObject aDocURL = LockFileCommon::ResolveLinks(INetURLObject(sOrigURL));
const OUString sExt = aDocURL.GetFileExtension();
if (isWordFormat(sExt))
eResult = AppType::Word;
else if (isExcelFormat(sExt))
eResult = AppType::Excel;
return eResult;
}
MSODocumentLockFile::MSODocumentLockFile(const OUString& aOrigURL)
: GenDocumentLockFile(GenerateMSOLockFileURL(aOrigURL))
, m_eAppType(getAppType(aOrigURL))
{
}
MSODocumentLockFile::~MSODocumentLockFile() {}
void MSODocumentLockFile::WriteEntryToStream(
const LockFileEntry& aEntry, const css::uno::Reference<css::io::XOutputStream>& xOutput)
......@@ -82,8 +90,8 @@ void MSODocumentLockFile::WriteEntryToStream(
::osl::MutexGuard aGuard(m_aMutex);
// Reallocate the date with the right size, different lock file size for different components
int nLockFileSize = isWordFormat(m_sOrigURL) ? MSO_WORD_LOCKFILE_SIZE
: MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE;
int nLockFileSize = m_eAppType == AppType::Word ? MSO_WORD_LOCKFILE_SIZE
: MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE;
css::uno::Sequence<sal_Int8> aData(nLockFileSize);
// Write out the user name's length as a single byte integer
......@@ -105,32 +113,26 @@ void MSODocumentLockFile::WriteEntryToStream(
}
// Fill up the remaining bytes with dummy data
if (isWordFormat(m_sOrigURL))
switch (m_eAppType)
{
while (nIndex < MSO_USERNAME_MAX_LENGTH + 2)
{
case AppType::Word:
while (nIndex < MSO_USERNAME_MAX_LENGTH + 2)
{
aData[nIndex] = static_cast<sal_Int8>(0);
++nIndex;
}
break;
case AppType::PowerPoint:
aData[nIndex] = static_cast<sal_Int8>(0);
++nIndex;
}
}
else if (isExcelFormat(m_sOrigURL))
{
while (nIndex < MSO_USERNAME_MAX_LENGTH + 3)
{
aData[nIndex] = static_cast<sal_Int8>(0x20);
++nIndex;
}
}
else
{
aData[nIndex] = static_cast<sal_Int8>(0);
++nIndex;
while (nIndex < MSO_USERNAME_MAX_LENGTH + 3)
{
aData[nIndex] = static_cast<sal_Int8>(0x20);
++nIndex;
}
[[fallthrough]];
case AppType::Excel:
while (nIndex < MSO_USERNAME_MAX_LENGTH + 3)
{
aData[nIndex] = static_cast<sal_Int8>(0x20);
++nIndex;
}
break;
}
// At the next position we have the user name's length again, but now as a 2 byte integer
......@@ -150,26 +152,28 @@ void MSODocumentLockFile::WriteEntryToStream(
}
// Fill the remaining part with dummy bits
if (isWordFormat(m_sOrigURL))
switch (m_eAppType)
{
while (nIndex < nLockFileSize)
{
aData[nIndex] = static_cast<sal_Int8>(0);
++nIndex;
}
}
else
{
while (nIndex < nLockFileSize)
{
aData[nIndex] = static_cast<sal_Int8>(0x20);
++nIndex;
if (nIndex < nLockFileSize)
case AppType::Word:
while (nIndex < nLockFileSize)
{
aData[nIndex] = static_cast<sal_Int8>(0);
++nIndex;
}
}
break;
case AppType::Excel:
case AppType::PowerPoint:
while (nIndex < nLockFileSize)
{
aData[nIndex] = static_cast<sal_Int8>(0x20);
++nIndex;
if (nIndex < nLockFileSize)
{
aData[nIndex] = static_cast<sal_Int8>(0);
++nIndex;
}
}
break;
}
xOutput->writeBytes(aData);
......@@ -248,7 +252,10 @@ void MSODocumentLockFile::RemoveFile()
bool MSODocumentLockFile::IsMSOSupportedFileFormat(const OUString& aURL)
{
return isWordFormat(aURL) || isExcelFormat(aURL) || isPowerPointFormat(aURL);
INetURLObject aDocURL = LockFileCommon::ResolveLinks(INetURLObject(aURL));
const OUString sExt = aDocURL.GetFileExtension();
return isWordFormat(sExt) || isExcelFormat(sExt) || isPowerPointFormat(sExt);
}
} // namespace svt
......
......@@ -53,7 +53,7 @@ namespace svt {
ShareControlFile::ShareControlFile( const OUString& aOrigURL )
: LockFileCommon( aOrigURL, ".~sharing." )
: LockFileCommon(GenerateOwnLockFileURL(aOrigURL, ".~sharing."))
{
if ( !m_xStream.is() && !GetURL().isEmpty() )
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment