text: /*
      ClockNWorkThread.cpp
  
      
  MIT License
  
  Copyright (c) 2016 cbtek
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
  
  */
  //----------------------------------------
  //ClockNWorkThread.cpp generated by cbtek on 11-15-2016 at 08:35:08 PM
  //----------------------------------------
  #include "ClockNWorkThread.h"
  
  #include "ClockNWorkManager.h"
  
  #include "utility/inc/FileUtils.hpp"
  
  #include <QProcess>
  
  #include <iostream>
  #include <sstream>
  
  using namespace cbtek::common::utility;
  namespace cbtek {
  namespace products {
  namespace productivity {
  namespace clock_n_work {
  
  
  ClockNWorkThread::ClockNWorkThread(const ClockNWorkSettings & settings)
  {
      m_settings = settings;
  
  }
  
  void ClockNWorkThread::run()
  {
      std::ostringstream command;
      QProcess process;
      int retcode = 0;
      bool isError = true;
      QString output;
      std::string file = FileUtils::getFileName(m_settings.getRemoteFilename());
      std::string path = StringUtils::remove(m_settings.getRemoteFilename(),file);
      std::string user = m_settings.getSshUserName();
  #ifdef __WIN32
      std::ostringstream batch;
      batch << "echo y | pscp\\pscp.exe -q -batch -pw \""<<ClockNWorkManager::inst().getSessionSSHPassword()<<"\" status.htm caja@cbtek.net:/home/caja/cbtek.net/cberry";
      FileUtils::writeFileContents("windows_ssh_batch.bat",batch.str());
      command << "windows_ssh_batch.bat";
  
      process.start(QString::fromStdString(command.str()));
      process.waitForFinished();
  
      FileUtils::deleteFile("windows_ssh_batch.bat");
  #else
      command << "sshpass -p \""<<ClockNWorkManager::inst().getSessionSSHPassword()<<"\" scp "+file+" "+user+"@"+m_settings.getSshRemoteHostName()+":"+path;
      //std::cerr << command.str()<<std::endl;
      process.start(QString::fromStdString(command.str()));
      process.waitForFinished();
  #endif    
      output = (process.readAllStandardError()+" "+process.readAllStandardOutput());
      output.replace("\n"," ");
      isError = (output.contains("error",Qt::CaseInsensitive) ||
                 output.contains("refused",Qt::CaseInsensitive) ||
                 output.contains("failed", Qt::CaseInsensitive) ||
                 output.contains("timeout", Qt::CaseInsensitive) ||
                 output.contains("not recognized", Qt::CaseInsensitive));
  
      std::string password = ClockNWorkManager::inst().getSessionSSHPassword();
      output.replace(QString::fromStdString(password),"******");
  
      if (isError)
      {
          emit failed(output);
      }
      else emit success();
  
  }
  
  ClockNWorkThread::~ClockNWorkThread()
  {
  
  }
  
  
  
  }}}}//end namespace
  
  
  