tdf#128502: Make _loKitDocument in class Document non-static again

It was made static in ea2b77ce07d615e72c29b7016b464a9147373805 for
Android's sake, so returning it to be per-instance is not a big thing.

Keep a separate static just for the Android app's use for now, while
the Android app supports just one open document at a time anyway. (It
is for the iOS app that I am moving towards supporting multiple open
documents at a time.)

Change-Id: I7fabeb21883eb7cd7155e880eb4cc0413124d1f8
Reviewed-on: https://gerrit.libreoffice.org/c/online/+/92625
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-by: Tor Lillqvist <tml@collabora.com>
diff --git a/android/lib/src/main/cpp/androidapp.cpp b/android/lib/src/main/cpp/androidapp.cpp
index defe7b6..a3f8471 100644
--- a/android/lib/src/main/cpp/androidapp.cpp
+++ b/android/lib/src/main/cpp/androidapp.cpp
@@ -340,7 +340,7 @@
    const char *fileUri = env->GetStringUTFChars(fileUri_, 0);
    const char *format = env->GetStringUTFChars(format_, 0);

    getLOKDocument()->saveAs(fileUri, format, nullptr);
    getLOKDocumentForAndroidOnly()->saveAs(fileUri, format, nullptr);

    env->ReleaseStringUTFChars(fileUri_, fileUri);
    env->ReleaseStringUTFChars(format_, format);
@@ -356,7 +356,7 @@
    if (arguments != nullptr)
        pArguments = pEnv->GetStringUTFChars(arguments, nullptr);

    getLOKDocument()->postUnoCommand(pCommand, pArguments, bNotifyWhenFinished);
    getLOKDocumentForAndroidOnly()->postUnoCommand(pCommand, pArguments, bNotifyWhenFinished);

    pEnv->ReleaseStringUTFChars(command, pCommand);
    if (arguments != nullptr)
@@ -402,9 +402,9 @@
    jclass class_LokClipboardData = env->GetObjectClass(lokClipboardData);
    jfieldID fieldId_LokClipboardData_clipboardEntries = env->GetFieldID(class_LokClipboardData , "clipboardEntries", "Ljava/util/ArrayList;");

    if (getLOKDocument()->getClipboard(mimeTypes,
                                       &outCount, &outMimeTypes,
                                       &outSizes, &outStreams))
    if (getLOKDocumentForAndroidOnly()->getClipboard(mimeTypes,
                                                     &outCount, &outMimeTypes,
                                                     &outSizes, &outStreams))
    {
        // return early
        if (outCount == 0)
@@ -440,9 +440,9 @@

    const char* mimeTypesHTML[] = { "text/plain;charset=utf-8", "text/html", nullptr };

    if (getLOKDocument()->getClipboard(mimeTypesHTML,
                                       &outCount, &outMimeTypes,
                                       &outSizes, &outStreams))
    if (getLOKDocumentForAndroidOnly()->getClipboard(mimeTypesHTML,
                                                     &outCount, &outMimeTypes,
                                                     &outSizes, &outStreams))
    {
        // return early
        if (outCount == 0)
@@ -522,7 +522,7 @@
        pStreams[nEntryIndex] = dataArray;
    }

    getLOKDocument()->setClipboard(nEntrySize, pMimeTypes, pSizes, pStreams);
    getLOKDocumentForAndroidOnly()->setClipboard(nEntrySize, pMimeTypes, pSizes, pStreams);
}

extern "C"
@@ -533,7 +533,7 @@
    size_t dataArrayLength = env->GetArrayLength(inData);
    char* dataArray = new char[dataArrayLength];
    env->GetByteArrayRegion(inData, 0, dataArrayLength, reinterpret_cast<jbyte*>(dataArray));
    getLOKDocument()->paste(mimeType, dataArray, dataArrayLength);
    getLOKDocumentForAndroidOnly()->paste(mimeType, dataArray, dataArrayLength);
    env->ReleaseStringUTFChars(inMimeType, mimeType);
}

diff --git a/kit/Kit.cpp b/kit/Kit.cpp
index 142392f6..a34b913 100644
--- a/kit/Kit.cpp
+++ b/kit/Kit.cpp
@@ -1428,6 +1428,9 @@
#ifdef IOS
            lok_document = nullptr;
#endif
#ifdef __ANDROID__
            _loKitDocumentForAndroidOnly.reset();
#endif
            _loKitDocument.reset();
            LOG_INF("Document [" << anonymizeUrl(_url) << "] session [" << sessionId << "] unloaded Document.");
            return;
@@ -1661,6 +1664,9 @@
            LOG_DBG("Calling lokit::documentLoad(" << FileUtil::anonymizeUrl(pURL) << ", \"" << options << "\").");
            const auto start = std::chrono::system_clock::now();
            _loKitDocument.reset(_loKit->documentLoad(pURL, options.c_str()));
#ifdef __ANDROID__
            _loKitDocumentForAndroidOnly = _loKitDocument;
#endif
            const auto duration = std::chrono::system_clock::now() - start;
            const auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
            const double totalTime = elapsed/1000.;
@@ -2056,7 +2062,10 @@
    std::string _jailedUrl;
    std::string _renderOpts;

    static std::shared_ptr<lok::Document> _loKitDocument;
    std::shared_ptr<lok::Document> _loKitDocument;
#ifdef __ANDROID__
    static std::shared_ptr<lok::Document> _loKitDocumentForAndroidOnly;
#endif
    std::shared_ptr<TileQueue> _tileQueue;
    std::shared_ptr<WebSocketHandler> _websocketHandler;

@@ -2089,17 +2098,22 @@
    /// For showing disconnected user info in the doc repair dialog.
    std::map<int, UserInfo> _sessionUserInfo;
    std::chrono::steady_clock::time_point _lastMemStatsTime;

    friend std::shared_ptr<lok::Document> getLOKDocument();
#ifdef __ANDROID__
    friend std::shared_ptr<lok::Document> getLOKDocumentForAndroidOnly();
#endif
};

std::shared_ptr<lok::Document> Document::_loKitDocument = std::shared_ptr<lok::Document>();
#ifdef __ANDROID__

std::shared_ptr<lok::Document> getLOKDocument()
std::shared_ptr<lok::Document> Document::_loKitDocumentForAndroidOnly = std::shared_ptr<lok::Document>();

std::shared_ptr<lok::Document> getLOKDocumentForAndroidOnly()
{
    return Document::_loKitDocument;
    return Document::_loKitDocumentForAndroidOnly;
}

#endif

class KitSocketPoll : public SocketPoll
{
    std::chrono::steady_clock::time_point _pollEnd;
diff --git a/kit/Kit.hpp b/kit/Kit.hpp
index b100fd6..1e60fdd 100644
--- a/kit/Kit.hpp
+++ b/kit/Kit.hpp
@@ -139,7 +139,9 @@
/// Anonymize usernames.
std::string anonymizeUsername(const std::string& username);

/// For the mobile, we need access to the document to perform eg. saveAs() for printing.
std::shared_ptr<lok::Document> getLOKDocument();
#ifdef __ANDROID__
/// For the Android app, for now, we need access to the one and only document open to perform eg. saveAs() for printing.
std::shared_ptr<lok::Document> getLOKDocumentForAndroidOnly();
#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */