<commit_before>/*
 * DesktopGwtCallback.cpp
 *
 * Copyright (C) 2009-11 by RStudio, Inc.
 *
 * This program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */

#include "DesktopGwtCallback.hpp"

#include <algorithm>

#if _WIN32
#include <shlobj.h>
#endif

#include <QtGui/QFileDialog>

#include <core/FilePath.hpp>
#include <core/system/System.hpp>

#include "DesktopAboutDialog.hpp"
#include "DesktopCRANMirrorDialog.hpp"
#include "DesktopOptionsDialog.hpp"
#include "DesktopOptions.hpp"
#include "DesktopBrowserWindow.hpp"
#include "DesktopWindowTracker.hpp"
#include "DesktopInputDialog.hpp"

using namespace core;

namespace desktop {

extern QString scratchPath;

void GwtCallback::browseUrl(QString url)
{
   QUrl qurl(url);

#ifdef Q_WS_MAC
   if (qurl.scheme() == "file")
   {
      QProcess open;
      open.start("open", QStringList() << url);
      open.waitForFinished(5000);
      if (open.exitCode() != 0)
      {
         // Probably means that the file doesn't have a registered
         // application or something.
         QProcess reveal;
         reveal.startDetached("open", QStringList() << "-R" << url);
      }
      return;
   }
#endif

   QDesktopServices::openUrl(qurl);
}

namespace {

FilePath userHomePath()
{
   return core::system::userHomePath("R_USER");
}

QString createAliasedPath(const QString& path)
{
   std::string aliased = FilePath::createAliasedPath(
         FilePath(path.toStdString()), userHomePath());
   return QString::fromStdString(aliased);
}

QString resolveAliasedPath(const QString& path)
{
   FilePath resolved(FilePath::resolveAliasedPath(path.toStdString(),
                                                  userHomePath()));
   return QString::fromStdString(resolved.absolutePath());
}

} // anonymous namespace

QString GwtCallback::getOpenFileName(const QString& caption,
                                    const QString& dir)
{
   QString resolvedDir = resolveAliasedPath(dir);
   QString result = QFileDialog::getOpenFileName(pOwnerWindow_, caption, resolvedDir);
   webView()->page()->mainFrame()->setFocus();
   return createAliasedPath(result);
}

QString GwtCallback::getSaveFileName(const QString& caption,
                                    const QString& dir)
{
   QString resolvedDir = resolveAliasedPath(dir);
   QString result = QFileDialog::getSaveFileName(pOwnerWindow_, caption, resolvedDir);
   webView()->page()->mainFrame()->setFocus();
   return createAliasedPath(result);
}

QString GwtCallback::getExistingDirectory(const QString& caption,
                                         const QString& dir)
{
   QString resolvedDir = resolveAliasedPath(dir);

   QString result;
#ifdef _WIN32
   if (!dir.isNull())
   {
      // Bug
      char szDir[MAX_PATH];
      BROWSEINFO bi;
      bi.hwndOwner = pOwnerWindow_->winId();
      bi.pidlRoot = NULL;
      bi.pszDisplayName = szDir;
      bi.lpszTitle = "Select a folder:";
      bi.ulFlags = BIF_RETURNONLYFSDIRS;
      bi.lpfn = NULL;
      bi.lpfn = 0;
      bi.iImage = -1;
      LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
      if (!pidl || !SHGetPathFromIDList(pidl, szDir))
         result = QString("");
      else
         result = QString::fromLocal8Bit(szDir);
   }
   else
   {
      result = QFileDialog::getExistingDirectory(pOwnerWindow_, caption);
   }
#else
   result = QFileDialog::getExistingDirectory(pOwnerWindow_, caption, resolvedDir);
#endif

   webView()->page()->mainFrame()->setFocus();
   return createAliasedPath(result);
}

void GwtCallback::doAction(QKeySequence::StandardKey key)
{
   QList<QKeySequence> bindings = QKeySequence::keyBindings(key);
   if (bindings.size() == 0)
      return;

   QKeySequence seq = bindings.first();

   int keyCode = seq[0];
   Qt::KeyboardModifier modifiers = static_cast<Qt::KeyboardModifier>(keyCode & Qt::KeyboardModifierMask);
   keyCode &= ~Qt::KeyboardModifierMask;

   QKeyEvent* keyEvent = new QKeyEvent(QKeyEvent::KeyPress, keyCode, modifiers);
   QCoreApplication::postEvent(webView(), keyEvent);
}<commit_msg>1071: For Move operation we should use the standard file picker on windows (part 2)<commit_after>/*
 * DesktopGwtCallback.cpp
 *
 * Copyright (C) 2009-11 by RStudio, Inc.
 *
 * This program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */

#include "DesktopGwtCallback.hpp"

#include <algorithm>

#if _WIN32
#include <shlobj.h>
#endif

#include <QtGui/QFileDialog>

#include <core/FilePath.hpp>
#include <core/system/System.hpp>

#include "DesktopAboutDialog.hpp"
#include "DesktopCRANMirrorDialog.hpp"
#include "DesktopOptionsDialog.hpp"
#include "DesktopOptions.hpp"
#include "DesktopBrowserWindow.hpp"
#include "DesktopWindowTracker.hpp"
#include "DesktopInputDialog.hpp"

using namespace core;

namespace desktop {

extern QString scratchPath;

void GwtCallback::browseUrl(QString url)
{
   QUrl qurl(url);

#ifdef Q_WS_MAC
   if (qurl.scheme() == "file")
   {
      QProcess open;
      open.start("open", QStringList() << url);
      open.waitForFinished(5000);
      if (open.exitCode() != 0)
      {
         // Probably means that the file doesn't have a registered
         // application or something.
         QProcess reveal;
         reveal.startDetached("open", QStringList() << "-R" << url);
      }
      return;
   }
#endif

   QDesktopServices::openUrl(qurl);
}

namespace {

FilePath userHomePath()
{
   return core::system::userHomePath("R_USER");
}

QString createAliasedPath(const QString& path)
{
   std::string aliased = FilePath::createAliasedPath(
         FilePath(path.toStdString()), userHomePath());
   return QString::fromStdString(aliased);
}

QString resolveAliasedPath(const QString& path)
{
   FilePath resolved(FilePath::resolveAliasedPath(path.toStdString(),
                                                  userHomePath()));
   return QString::fromStdString(resolved.absolutePath());
}

} // anonymous namespace

QString GwtCallback::getOpenFileName(const QString& caption,
                                    const QString& dir)
{
   QString resolvedDir = resolveAliasedPath(dir);
   QString result = QFileDialog::getOpenFileName(pOwnerWindow_, caption, resolvedDir);
   webView()->page()->mainFrame()->setFocus();
   return createAliasedPath(result);
}

QString GwtCallback::getSaveFileName(const QString& caption,
                                    const QString& dir)
{
   QString resolvedDir = resolveAliasedPath(dir);
   QString result = QFileDialog::getSaveFileName(pOwnerWindow_, caption, resolvedDir);
   webView()->page()->mainFrame()->setFocus();
   return createAliasedPath(result);
}

QString GwtCallback::getExistingDirectory(const QString& caption,
                                         const QString& dir)
{
   QString resolvedDir = resolveAliasedPath(dir);

   QString result;
#ifdef _WIN32
   if (dir.isNull())
   {
      // Bug
      char szDir[MAX_PATH];
      BROWSEINFO bi;
      bi.hwndOwner = pOwnerWindow_->winId();
      bi.pidlRoot = NULL;
      bi.pszDisplayName = szDir;
      bi.lpszTitle = "Select a folder:";
      bi.ulFlags = BIF_RETURNONLYFSDIRS;
      bi.lpfn = NULL;
      bi.lpfn = 0;
      bi.iImage = -1;
      LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
      if (!pidl || !SHGetPathFromIDList(pidl, szDir))
         result = QString("");
      else
         result = QString::fromLocal8Bit(szDir);
   }
   else
   {
      result = QFileDialog::getExistingDirectory(pOwnerWindow_, caption, resolvedDir);
   }
#else
   result = QFileDialog::getExistingDirectory(pOwnerWindow_, caption, resolvedDir);
#endif

   webView()->page()->mainFrame()->setFocus();
   return createAliasedPath(result);
}

void GwtCallback::doAction(QKeySequence::StandardKey key)
{
   QList<QKeySequence> bindings = QKeySequence::keyBindings(key);
   if (bindings.size() == 0)
      return;

   QKeySequence seq = bindings.first();

   int keyCode = seq[0];
   Qt::KeyboardModifier modifiers = static_cast<Qt::KeyboardModifier>(keyCode & Qt::KeyboardModifierMask);
   keyCode &= ~Qt::KeyboardModifierMask;

   QKeyEvent* keyEvent = new QKeyEvent(QKeyEvent::KeyPress, keyCode, modifiers);
   QCoreApplication::postEvent(webView(), keyEvent);
}
{'fraction_non_alphanumeric': 0.11861104744441897, 'fraction_numerical': 0.005844602337840935, 'mean_word_length': 3.5548016701461376, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4611549', 'n_tokens_mistral': 2678, 'n_tokens_neox': 2608, 'n_words': 597}