Click here to Skip to main content
15,886,806 members
Articles / Desktop Programming / MFC

A Google Docs Based Backup Application

Rate me:
Please Sign up or sign in to vote.
4.89/5 (71 votes)
20 Mar 2015CPOL4 min read 98.6K   3.9K   121  
A C++ client for uploading documents to your Google Docs account
#pragma once

#include "Singleton.h"
#include "SmartHTTP.h"
#include "GoogleFolderTree.h"

class CGoogleUploader : 
  public Singleton<CGoogleUploader>
{
public:
  typedef enum { UP_NONE, UP_LOGIN, UP_FOLDERS, UP_CHECKDUPLICATE, UP_SENDING, UP_END } UPLOAD_STEP;
  typedef enum 
  { 
    UR_ERROR,                       // error
    UR_CRITICALERROR,               // critical error is an error for which operation retry does not make sense
    UR_SUCCESS, 
    UR_ABORT, 
  } RESULT;
  typedef struct
  {
    RESULT res;
    wstring errorMessage; // contains program error message or server error code, may be empty
    wstring serverMessage; // contains server error message, may be empty
    void clear()
    {
      res = UR_ERROR;
      errorMessage.clear();
      serverMessage.clear();
    }
  } RESULT_INFO;

  CGoogleUploader(void);
  ~CGoogleUploader(void);
  void Initialize(HWND notifyWindow);
  BOOL BeginFileUpload(const wstring &auth, const wstring &googleFolderId, 
    const wstring &user, const wstring &password, BOOL convert, 
    const wstring &path, const wstring &googlePath);
  void EndFileUpload(RESULT_INFO &resInfo, wstring &auth, wstring &newTitle, wstring &googleFolderId, BOOL abort);
  void GetUploadStatus(UPLOAD_STEP &uploadStep, int &percent);
  
  static BOOL IsFileAllowed(const wchar_t *fileExt, __int64 fileSize);
private:
  typedef struct 
  {
    CSmartHTTP::RESULT res;
    DWORD winInetError;
    DWORD httpStatus;
  } INTERNAL_RESULT;

  CSmartHTTP m_internet;
  HWND m_notifyWindow;
  HANDLE m_uploadThread;
  HANDLE m_abortEvent;

  UPLOAD_STEP m_uploadStep;
  RESULT_INFO m_resInfo;

  wstring m_newTitle;
  wstring m_auth;
  wstring m_user;
  wstring m_password;
  wstring m_path;
  wstring m_googlePath;
  wstring m_googleFolderId;
  BOOL m_convert;

  CRITICAL_SECTION m_cs;
  __int64 m_postToSend;

  static wstring BuildFileUploadHeader(const wstring &auth, const wstring &googleTitle, const wstring &path, __int64 fileSize, BOOL convert);
  static DWORD WINAPI StaticUploadThreadProc(LPVOID lpParameter);

  void UploadThreadProc();
  void Upload(RESULT_INFO &resInfo, OUT wstring &serverTitle, IN OUT wstring &auth, IN OUT wstring &googleFolderId, const wstring &user, const wstring &password, const wstring &googlePath, const wstring &path, BOOL convert);
  BOOL SendFile(RESULT_INFO &resInfo, const wstring &auth, const wstring &googleTitle, const wstring &googleFolderId, const wstring &path, BOOL convert);
  BOOL Login(RESULT_INFO &resInfo, wstring &auth, const wstring &user, const wstring &password);
  void SetUploadStep(UPLOAD_STEP uploadStep);
  void FormatInternalResult(RESULT_INFO &resInfo, const INTERNAL_RESULT &rr);
  BOOL CreateFolder(RESULT_INFO &resInfo, wstring &folderId, const wstring &auth, const wstring &folderName, const wstring &parentFolderId);
  BOOL ProvideGooglePath(RESULT_INFO &resInfo, BOOL &folderCreated, wstring &folderId, const wstring &auth, const wstring &googlePath);
  BOOL GDocsRequest(INTERNAL_RESULT &ir, streambuf &reply, LPCTSTR verb, LPCTSTR engine, LPCTSTR header, 
    streambuf *content = NULL, __int64 contentLength = 0 );
  BOOL GetFolderList(RESULT_INFO &resInfo, GOOGLE_FOLDER_DESCR &googleFolderTree, const wstring &auth);
  void FormatHTTPError( RESULT_INFO &resInfo, DWORD httpStatus, const string &serverResponse);
  BOOL CreateGooglePath(RESULT_INFO &resInfo, wstring &finalId, const wstring &auth, const path_vector &pathWithCase, size_t startAt, const wstring &parentId);
  BOOL GetFolderFiles(RESULT_INFO &resInfo, vector<GOOGLE_FILE_DESCR> &files, const wstring &auth, const wstring &googleFolderId);
  BOOL FilesMatch(RESULT_INFO &resInfo, BOOL &match, const wstring &auth, const wstring &fileUrl, const wstring &path);
  BOOL CheckFileCopy(RESULT_INFO &resInfo, OUT BOOL &fileOnServer, OUT wstring &newTitle, IN const wstring &auth, IN const wstring &fileTitle, IN const wstring &googleFolderId, IN const wstring &path);
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Secured Globe, Inc.
United States United States
Michael Haephrati is a music composer, an inventor and an expert specializes in software development and information security, who has built a unique perspective which combines technology and the end user experience. He is the author of a the book Learning C++ , which teaches C++ 20, and was published in August 2022.

He is the CEO of Secured Globe, Inc., and also active at Stack Overflow.

Read our Corporate blog or read my Personal blog.





Comments and Discussions