/** * Get a text an remove the single lines out of it/ * TODO: Move this to the am_string namespace? * @param const MYODD_STRING& szCommandLine the text that we would like to enforce into a single line. * @return MYODD_STRING a single line of text. */ MYODD_STRING Action::toSingleLine( LPCTSTR sText ) const { // Sanity checks. if(nullptr == sText ) { return _T(""); } LPCTSTR pdest = _tcschr( sText, '\n' ); size_t result; MYODD_STRING ret( sText ); if( pdest != nullptr) { result = pdest - sText; ret = ret.substr( 0, result ); return toSingleLine( ret.c_str() ); } pdest = _tcschr( sText, '\r' ); if( pdest != nullptr) { result = pdest - sText; ret = ret.substr( 0, result ); return toSingleLine( ret.c_str() ); } return ret; }
/** * Try and do it when we have no command line * @param CWnd* pWnd the last forground window. * @param bool isPrivileged if this action is privileged or not. * @return bool success or not. */ ActiveAction* Action::CreateActiveActionWithNoCommandLine(CWnd* pWnd, bool isPrivileged ) const { // the command line we will try and make. MYODD_STRING szCommandLine = _T(""); size_t maxClipboardMemory = CActionMonitorApp::GetMaxClipboardMemory(); // // we need to wrap the whole clipboard around try/catch as not all clipboard cases have been tested // I try to restore data to the clipboard without really knowing if the data itself is valid // so that can cause some kind of problem(s) try { // ask the system if anything was copied. // the clipboard function will ask us to get the most likely text // there will probably only be a conflict with explorer, (of any flavor) // that could copy text and/or file names. // CWnd* cwnd = CActionMonitorApp::GetLastForegroundWindow(); Clipboard clipboard( cwnd, maxClipboardMemory ); // any other values are rejected, (bitmaps and so on). MYODD_STRING sText = _T(""); if (clipboard.GetTextFromClipboard(sText, true )) { // we need to trim all the items into one single line // because command lines cannot accept multiple lines // // or we might need to replace certain characters. szCommandLine = toSingleLine(sText.c_str()); } } catch (...) { szCommandLine = _T(""); myodd::log::LogError(_T("Critical error while trying to run an action, [%s]."), Command() ); _ASSERT(0); // the main reason for failure is probably because // there is a format in the Clipboard that I am not handling properly // there should be a way of sending me a mail when this happens so we can look into fixing it. } // we can now do it direct. return CreateActiveActionDirect( pWnd, szCommandLine.c_str(), isPrivileged); }
errInfo newWork(const std::wstring &name, const std::wstring &info, work **ret) { dataBuf buf; CURL *handle = curl_easy_init(); std::string postField = "field=work&operation=add&name=" + encode(name) + "&info=" + encode(toSingleLine(info)); std::unique_ptr<char[]> errBuf = std::make_unique<char[]>(2048); CURLcode success; curl_easy_setopt(handle, CURLOPT_URL, scriptURL); curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errBuf.get()); curl_easy_setopt(handle, CURLOPT_POSTFIELDS, postField.c_str()); curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(handle, CURLOPT_WRITEDATA, &buf); success = curl_easy_perform(handle); if (success != CURLcode::CURLE_OK) { curl_easy_cleanup(handle); std::string err(errBuf.get()); return errInfo("E:network:" + err); } curl_easy_cleanup(handle); size_t newID; if (str2num(buf, newID) != 0) return errInfo(std::string("E:Server side error:") + buf); *ret = new work(newID, name, info); workList.emplace(newID, *ret); return errInfo(); }
errInfo work::editInfo(const std::wstring &newInfo) { info = newInfo; dataBuf buf; CURL *handle = curl_easy_init(); std::string postField = "field=work&operation=edit&id=" + std::to_string(wID) + "&item=info&value=" + encode(toSingleLine(newInfo)); std::unique_ptr<char[]> errBuf = std::make_unique<char[]>(2048); CURLcode success; curl_easy_setopt(handle, CURLOPT_URL, scriptURL); curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errBuf.get()); curl_easy_setopt(handle, CURLOPT_POSTFIELDS, postField.c_str()); curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(handle, CURLOPT_WRITEDATA, &buf); success = curl_easy_perform(handle); if (success != CURLcode::CURLE_OK) { curl_easy_cleanup(handle); std::string err(errBuf.get()); return errInfo("E:network:" + err); } curl_easy_cleanup(handle); if (!buf.empty()) return errInfo(std::string("E:Server side error:") + buf); return errInfo(); }