public bool Write(FileFolderInfo fileFolderInfo)
 {
     return ExecuteNonQuery(fileFolderInfo);
 }
        public bool ExecuteNonQuery(FileFolderInfo fileFolderInfo)
        {
            try
            {
                string query = @"INSERT INTO " + TABLE_NAME + " VALUES(" +
                                "@Key ," +
                                "@ModifiedDate , " +
                                "@CreatedDate  ," +
                                "@FileSize  ," +
                                "@ContentUrl , " +
                                "@ParentUrl , " +
                                "@ETag , " +
                                "@FileName , " +
                                "@MimeType , " +
                                "@IsPublic , " +
                                "@IsShared , " +
                                "@Status , " +
                                "@Type , " +
                                "@ParentDir )";

                SQLiteConnection sqlConnection = OpenConnection();
                SQLiteCommand sqlCommand = new SQLiteCommand(query, sqlConnection);

                sqlCommand.Parameters.Add(new SQLiteParameter("@Key",System.Data.DbType.String));
                sqlCommand.Parameters.Add(new SQLiteParameter("@ModifiedDate",System.Data.DbType.DateTime));
                sqlCommand.Parameters.Add(new SQLiteParameter("@CreatedDate", System.Data.DbType.DateTime));
                sqlCommand.Parameters.Add(new SQLiteParameter("@FileSize", System.Data.DbType.Double));
                sqlCommand.Parameters.Add(new SQLiteParameter("@ContentUrl", System.Data.DbType.String));
                sqlCommand.Parameters.Add(new SQLiteParameter("@ParentUrl", System.Data.DbType.String));
                sqlCommand.Parameters.Add(new SQLiteParameter("@FileName", System.Data.DbType.String));
                sqlCommand.Parameters.Add(new SQLiteParameter("@MimeType", System.Data.DbType.String));
                sqlCommand.Parameters.Add(new SQLiteParameter("@IsPublic", System.Data.DbType.Boolean));
                sqlCommand.Parameters.Add(new SQLiteParameter("@IsShared", System.Data.DbType.Boolean));
                sqlCommand.Parameters.Add(new SQLiteParameter("@Status",System.Data.DbType.String));
                sqlCommand.Parameters.Add(new SQLiteParameter("@Type",System.Data.DbType.String));
                sqlCommand.Parameters.Add(new SQLiteParameter("@ParentDir",System.Data.DbType.String));
                sqlCommand.Parameters.Add(new SQLiteParameter("@ETag",System.Data.DbType.String));

                sqlCommand.Parameters["@Key"].Value = fileFolderInfo.Key;
                sqlCommand.Parameters["@ModifiedDate"].Value = fileFolderInfo.ModifiedDate;
                sqlCommand.Parameters["@CreatedDate"].Value = fileFolderInfo.CreatedDate;
                sqlCommand.Parameters["@FileSize"].Value = fileFolderInfo.FileSize;
                sqlCommand.Parameters["@ContentUrl"].Value = fileFolderInfo.ContentUrl;
                sqlCommand.Parameters["@ParentUrl"].Value = fileFolderInfo.ParentUrl;
                sqlCommand.Parameters["@FileName"].Value = fileFolderInfo.FileName;
                sqlCommand.Parameters["@MimeType"].Value = fileFolderInfo.MimeType;
                sqlCommand.Parameters["@IsPublic"].Value = fileFolderInfo.IsPublic;
                sqlCommand.Parameters["@IsShared"].Value = fileFolderInfo.IsShared;
                sqlCommand.Parameters["@Status"].Value = fileFolderInfo.Status;
                sqlCommand.Parameters["@Type"].Value = fileFolderInfo.Type;
                sqlCommand.Parameters["@ParentDir"].Value = fileFolderInfo.ParentDir;
                sqlCommand.Parameters["@ETag"].Value = fileFolderInfo.ETag;

                sqlCommand.ExecuteNonQuery();
                sqlConnection.Close();
                return true;
            }
            catch (Exception ex)
            {
                LogWrapper.LogMessage("DbHandler - ExecuteNonQuery", "Caught exception: " + ex.Message);
                return false;
            }
        }
        private int UpdateFromNQ(NQDetails UpdateQ)
        {
            //LogWrapper.LogMessage("frmSyncManager - UpdateFromNQ - ", "Enter");

            NQDetails nqDetail = UpdateQ;
            int nStatus = 0;

            int nStatusCode = 0;
            NSResult nsResult = null;

            //Encoding uri for special character and then replacing "+" with "%20" if we have spaces in file name
            string uri = cLoginDetails.szNamespaceUri + "/" + HttpUtility.UrlEncode(nqDetail.StrMezeoExportedPath);
            uri = uri.Replace("+", "%20");

            //nsResult = cMezeoFileCloud.GetNamespaceResult(cLoginDetails.szNamespaceUri + "/" +
            //                                                nqDetail.StrMezeoExportedPath,
            //                                                nqDetail.StrObjectType, ref nStatusCode);

            nsResult = cMezeoFileCloud.GetNamespaceResult(uri, nqDetail.StrObjectType, ref nStatusCode);
            if (nStatusCode == ResponseCode.LOGINFAILED1 || nStatusCode == ResponseCode.LOGINFAILED2)
            {
                return nStatusCode;
            }
            else if (nStatusCode != ResponseCode.GETNAMESPACERESULT && nStatusCode != ResponseCode.NOTFOUND)
            {
                return nStatusCode;
            }

            if (nsResult == null && nqDetail.StrEvent != "cdmi_delete")
            {
                //LogWrapper.LogMessage("frmSyncManager - UpdateFromNQ - ", "nsResult Null");
                nStatus = 1;
                return nStatus;
            }

            if (nqDetail.StrObjectName == "csp_recyclebin")
            {
                //LogWrapper.LogMessage("frmSyncManager - UpdateFromNQ - skipping csp_recyclebin notification.", "csp_recyclebin notification skipped");
                nStatus = 1;
                return nStatus;
            }

            int startIndex = Math.Min(cLoginDetails.szNQParentUri.Length + 1, nqDetail.StrParentUri.Length);
            string strPath = nqDetail.StrParentUri.Substring(startIndex);
            string strKey = strPath.Replace("/" , "\\");

            if(nsResult == null)
                strKey += nqDetail.StrObjectName;
            else
                strKey += nsResult.StrName;

            strPath = BasicInfo.SyncDirPath + "\\" + strKey;
            //lblStatusL3.Text = strPath;

            if (nqDetail.StrEvent == "cdmi_create_complete")
            {
                //LogWrapper.LogMessage("frmSyncManager - UpdateFromNQ - ", nqDetail.StrEvent + " - " + strKey + " - Enter");

                string strDBKey = dbHandler.GetString(DbHandler.TABLE_NAME, DbHandler.KEY, new string[] { DbHandler.CONTENT_URL }, new string[] { nsResult.StrContentsUri }, new DbType[] { DbType.String });

                // Just because the length is 0 doesn't make it the only reason to download the file.
                // Check the file size against the event.  If the size differs, then download the file
                // again since the local one is a partial...probably from a pause/resume.
                // The pause/resume can also result in a file that doesn't exist even though the database
                // entry does.  That condition should also be marked as a difference so that the event
                // is processed and the file is downloaded.
                bool bLocalFileDiffers = false;
                if (strDBKey.Trim().Length != 0)
                {
                    try
                    {
                        FileInfo fileInfo = new FileInfo(strPath);
                        if ((fileInfo != null) && (0 == (fileInfo.Attributes & FileAttributes.Directory)) && fileInfo.Exists && (fileInfo.Length != nqDetail.lSize))
                            bLocalFileDiffers = true;
                        if ((0 == (fileInfo.Attributes & FileAttributes.Directory)) && (!System.IO.File.Exists(strPath)))
                            bLocalFileDiffers = true;
                    }
                    catch (System.IO.PathTooLongException ex)
                    {
                        // Skip this item.
                        LogWrapper.LogMessage("frmSyncManager - UpdateFromNQ", "Caught exception: " + ex.Message);
                        nStatus = 1;
                        return nStatus;
                    }
                }

                if ((strDBKey.Trim().Length == 0) || bLocalFileDiffers)
                {
                    nStatus = nqEventCdmiCreate(nqDetail, nsResult, strKey, strPath);
                    if (nStatus == ResponseCode.LOGINFAILED1 || nStatus == ResponseCode.LOGINFAILED2)
                    {
                        return nStatus;
                    }
                    else if (nStatus != ResponseCode.GETETAG && nStatus != ResponseCode.DOWNLOADFILE && nStatus != ResponseCode.DOWNLOADITEMDETAILS && nStatus != 1)
                    {
                        return nStatus;
                    }
                }
                else
                    nStatus = 1;

                //LogWrapper.LogMessage("frmSyncManager - UpdateFromNQ - ", nqDetail.StrEvent + " - " + strKey + " - Leave");
            }
            else if (nqDetail.StrEvent == "cdmi_modify_complete")
            {
                //LogWrapper.LogMessage("frmSyncManager - UpdateFromNQ - ", nqDetail.StrEvent + " - " + strKey + " - Enter");

                string strDBKey = dbHandler.GetString(DbHandler.TABLE_NAME, DbHandler.KEY, new string[] { DbHandler.CONTENT_URL }, new string[] { nsResult.StrContentsUri }, new DbType[] { DbType.String });
                string strDBEtag = dbHandler.GetString(DbHandler.TABLE_NAME, DbHandler.E_TAG, new string[] { DbHandler.KEY }, new string[] { strDBKey }, new DbType[] { DbType.String });
                string strEtagCloud = cMezeoFileCloud.GetETag(nsResult.StrContentsUri, ref nStatusCode);
                if (nStatusCode == ResponseCode.LOGINFAILED1 || nStatusCode == ResponseCode.LOGINFAILED2)
                {
                    return nStatusCode;
                }
                else if (nStatusCode != ResponseCode.GETETAG)
                {
                    return nStatusCode;
                }

                if (strKey != strDBKey)
                {
                    if (File.Exists(strPath))
                    {
                        nqEventCdmiDelete(BasicInfo.SyncDirPath + "\\" + strDBKey, strDBKey);
                    }
                    else
                    {
                        if (File.Exists(BasicInfo.SyncDirPath + "\\" + strDBKey))
                            File.Move(BasicInfo.SyncDirPath + "\\" + strDBKey, strPath);
                    }

                    // Let's update the name as well.
                    string fileName = strPath.Substring(strPath.LastIndexOf("\\") + 1);
                    dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.FILE_NAME, fileName, DbHandler.KEY, strDBKey);

                    // Update the key for the entry.
                    dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.KEY, strKey, DbHandler.KEY, strDBKey);
                }

                if (strDBEtag != strEtagCloud)
                {
                    if ("FILE" == nsResult.StrType)
                    {
                        bool bRet = cMezeoFileCloud.DownloadFile(nsResult.StrContentsUri + '/' + nsResult.StrName, strPath, nsResult.dblSizeInBytes, ref nStatusCode);
                        if (nStatusCode == ResponseCode.LOGINFAILED1 || nStatusCode == ResponseCode.LOGINFAILED2)
                        {
                            return nStatusCode;
                        }
                        else if (nStatusCode != ResponseCode.DOWNLOADFILE)
                        {
                            return nStatusCode;
                        }
                        if (bRet)
                        {
                            dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.E_TAG, strEtagCloud, DbHandler.KEY, strKey);
                            bool isFileExist = File.Exists(strPath);
                            if (isFileExist)
                            {
                                FileInfo fInfo = new FileInfo(strPath);
                                dbHandler.UpdateModifiedDate(fInfo.LastWriteTime, strKey);
                            }
                        }
                    }
                }
                nStatus = 1;
                //LogWrapper.LogMessage("frmSyncManager - UpdateFromNQ - ", nqDetail.StrEvent + " - " + strKey + " - Leave");
            }
            else if (nqDetail.StrEvent == "cdmi_delete")
            {
                //LogWrapper.LogMessage("frmSyncManager - UpdateFromNQ - ", nqDetail.StrEvent + " - " + strKey + " - Enter");

                // bugzid:2352 - file is open when app gets cloud event to delete same file
                // If the file can't be deleted (due to being in use), then just keep retrying
                // until the file is released.
                if (nqEventCdmiDelete(strPath, strKey))
                    nStatus = 1;
                else
                    nStatus = 0;

                //LogWrapper.LogMessage("frmSyncManager - UpdateFromNQ - ", nqDetail.StrEvent + " - " + strKey + " - Leave");
            }
            else if (nqDetail.StrEvent == "cdmi_rename")
            {
                //LogWrapper.LogMessage("frmSyncManager - UpdateFromNQ - ", nqDetail.StrEvent + " - " + strKey + " - Enter");

                string strDBKey = dbHandler.GetString(DbHandler.TABLE_NAME, DbHandler.KEY, new string[] { DbHandler.CONTENT_URL }, new string[] { nsResult.StrContentsUri }, new DbType[] { DbType.String });
                if (strDBKey.Trim().Length != 0 && strDBKey != strKey)
                {
                    if(Directory.Exists(strPath))
                    {
                        nqEventCdmiDelete(BasicInfo.SyncDirPath + "\\" + strDBKey, strDBKey);
                    }
                    else
                    {
                        // Let's update the name as well.
                        string fileName = strPath.Substring(strPath.LastIndexOf("\\") + 1);
                        dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.FILE_NAME, fileName, DbHandler.KEY, strDBKey);

                        // Update the key for the entry.
                        Directory.Move(BasicInfo.SyncDirPath + "\\" + strDBKey, strPath);
                        dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.KEY , strKey , DbHandler.KEY , strDBKey );

                        // Update the key for the children.
                        DirectoryInfo rootDir = new DirectoryInfo(strPath);
                        WalkDirectoryTree(rootDir, BasicInfo.SyncDirPath + "\\" + strDBKey);
                    }
                }
                else if(strDBKey.Trim().Length == 0)
                {
                    nStatus = nqEventCdmiCreate(nqDetail, nsResult, strKey, strPath);
                    if (nStatus == ResponseCode.LOGINFAILED1 || nStatus == ResponseCode.LOGINFAILED2)
                    {
                        return nStatus;
                    }
                    else if (nStatus != ResponseCode.GETETAG && nStatus != ResponseCode.DOWNLOADFILE && nStatus != ResponseCode.DOWNLOADITEMDETAILS && nStatus != 1)
                    {
                        return nStatus;
                    }
                }
                nStatus = 1;
                //LogWrapper.LogMessage("frmSyncManager - UpdateFromNQ - ", nqDetail.StrEvent + " - " + strKey + " - Leave");
            }
            else if (nqDetail.StrEvent == "cdmi_copy")
            {
                //LogWrapper.LogMessage("frmSyncManager - UpdateFromNQ - ", nqDetail.StrEvent + " - " + strKey + " - Enter");

                if (strKey.LastIndexOf("\\") != -1)
                    CheckAndCreateForParentDir(strKey.Substring(0, strKey.LastIndexOf("\\")));

                FileFolderInfo fileFolderInfo = new FileFolderInfo();

                fileFolderInfo.IsPublic = nsResult.bPublic;
                fileFolderInfo.IsShared = nsResult.bShared;
                fileFolderInfo.ContentUrl = nsResult.StrContentsUri;
                fileFolderInfo.CreatedDate = nsResult.dtCreated;
                fileFolderInfo.FileName = nsResult.StrName;
                fileFolderInfo.FileSize = nsResult.dblSizeInBytes;
                fileFolderInfo.MimeType = nsResult.StrMimeType;
                fileFolderInfo.ModifiedDate = nsResult.dtModified;
                fileFolderInfo.ParentUrl = nsResult.StrParentUri;
                fileFolderInfo.Status = DB_STATUS_SUCCESS;
                fileFolderInfo.Type = nsResult.StrType;
                fileFolderInfo.Key = strKey;

                int lastSepIndex = strKey.LastIndexOf("\\");
                string parentDirPath = "";

                if (lastSepIndex != -1)
                {
                    parentDirPath = strKey.Substring(0, strKey.LastIndexOf("\\"));
                    parentDirPath = parentDirPath.Substring(parentDirPath.LastIndexOf("\\") + 1);
                }

                fileFolderInfo.ParentDir = parentDirPath;
                bool bRet = false;
                if (nqDetail.StrObjectType == "DIRECTORY")
                {
                    if (Directory.Exists(strPath))
                    {
                        //string strCheck = dbHandler.GetString(DbHandler.TABLE_NAME, DbHandler.KEY, DbHandler.KEY + "='" + strKey + "'");
                        string strCheck = dbHandler.GetString(DbHandler.TABLE_NAME, DbHandler.KEY, new string[] { DbHandler.KEY }, new string[] { strKey }, new DbType[] { DbType.String });
                        if(strCheck == strKey)
                            bRet = false;
                        else
                            bRet = true;
                    }
                    else
                    {
                        Directory.CreateDirectory(strPath);
                        bRet = true;
                    }
                }
                else
                {
                    if (File.Exists(strPath))
                    {
                        bRet = false;
                    }
                    else
                    {
                        string strEtagCloud = cMezeoFileCloud.GetETag(nsResult.StrContentsUri, ref nStatusCode);
                        if (nStatusCode == ResponseCode.LOGINFAILED1 || nStatusCode == ResponseCode.LOGINFAILED2)
                        {
                            return nStatusCode;
                        }
                        else if (nStatusCode != ResponseCode.GETETAG)
                        {
                            return nStatusCode;
                        }
                        //string strCheck = dbHandler.GetString(DbHandler.TABLE_NAME, DbHandler.KEY, DbHandler.E_TAG + "='" + strEtagCloud + "'");
                        string strCheck = dbHandler.GetString(DbHandler.TABLE_NAME, DbHandler.KEY, new string[] { DbHandler.E_TAG }, new string[] { strEtagCloud }, new DbType[] { DbType.String });
                        if (strCheck.Trim().Length != 0)
                        {
                            File.Copy(BasicInfo.SyncDirPath + "\\" + strCheck, strPath);
                            fileFolderInfo.ETag = strEtagCloud;
                            bRet = true;
                        }
                        else
                        {
                            bRet = cMezeoFileCloud.DownloadFile(nsResult.StrContentsUri + '/' + nsResult.StrName, strPath, nsResult.dblSizeInBytes, ref nStatusCode);
                            if (nStatusCode == ResponseCode.LOGINFAILED1 || nStatusCode == ResponseCode.LOGINFAILED2)
                            {
                                return nStatusCode;
                            }
                            else if (nStatusCode != ResponseCode.DOWNLOADFILE)
                            {
                                return nStatusCode;
                            }
                        }
                    }
                }

                if (bRet)
                {
                    if (fileFolderInfo.ETag == null)
                    {
                        fileFolderInfo.ETag = "";
                    }

                    if (fileFolderInfo.ETag.Trim().Length == 0)
                        fileFolderInfo.ETag = cMezeoFileCloud.GetETag(nsResult.StrContentsUri, ref nStatusCode);

                    if (fileFolderInfo.ETag == null)
                    {
                        fileFolderInfo.ETag = "";
                    }
                    if (fileFolderInfo.MimeType == null)
                    {
                        fileFolderInfo.MimeType = "";
                    }

                    dbHandler.Write(fileFolderInfo);
                }
                nStatus = 1;
                //LogWrapper.LogMessage("frmSyncManager - UpdateFromNQ - ", nqDetail.StrEvent + " - " + strKey + " - Leave");
            }

            //LogWrapper.LogMessage("frmSyncManager - UpdateFromNQ - ", "Leave");

            return nStatus;
        }
        private int nqEventCdmiCreate(NQDetails nqDetail, NSResult nsResult, string strKey, string strPath)
        {
            //LogWrapper.LogMessage("frmSyncManager - nqEventCdmiCreate", "enter");
            //bool bIssuccess = false;
            int nStatus = 0;
            FileFolderInfo fileFolderInfo = new FileFolderInfo();

            fileFolderInfo.IsPublic = nsResult.bPublic;
            fileFolderInfo.IsShared = nsResult.bShared;
            fileFolderInfo.ContentUrl = nsResult.StrContentsUri;
            fileFolderInfo.CreatedDate = nsResult.dtCreated;
            fileFolderInfo.FileName = nsResult.StrName;
            fileFolderInfo.FileSize = nsResult.dblSizeInBytes;
            fileFolderInfo.MimeType = nsResult.StrMimeType;
            fileFolderInfo.ModifiedDate = nsResult.dtModified;
            fileFolderInfo.ParentUrl = nsResult.StrParentUri;
            fileFolderInfo.Status = DB_STATUS_IN_PROGRESS;
            fileFolderInfo.Type = nsResult.StrType;
            fileFolderInfo.Key = strKey;

            int lastSepIndex = strKey.LastIndexOf("\\");
            string parentDirPath = "";

            if (lastSepIndex != -1)
            {
                parentDirPath = strKey.Substring(0, strKey.LastIndexOf("\\"));
                parentDirPath = parentDirPath.Substring(parentDirPath.LastIndexOf("\\") + 1);
            }

            fileFolderInfo.ParentDir = parentDirPath;

            if (fileFolderInfo.ETag == null) { fileFolderInfo.ETag = ""; }
            if (fileFolderInfo.MimeType == null) { fileFolderInfo.MimeType = ""; }

            dbHandler.Write(fileFolderInfo);

            bool bRet = false;
            string strEtag = "";
            int refCode = 0;
            int nStatusCode = 0;

            if (nqDetail.StrObjectType == "FILE")
            {
                MarkParentsStatus(strPath, DB_STATUS_IN_PROGRESS);
                bRet = cMezeoFileCloud.DownloadFile(nsResult.StrContentsUri + '/' + nsResult.StrName, strPath,nsResult.dblSizeInBytes, ref nStatusCode);
                if (nStatusCode == ResponseCode.LOGINFAILED1 || nStatusCode == ResponseCode.LOGINFAILED2)
                {
                    return nStatusCode;
                }
                else if (nStatusCode != ResponseCode.DOWNLOADFILE)
                {
                    return nStatusCode;
                }
                strEtag = cMezeoFileCloud.GetETag(nsResult.StrContentsUri, ref refCode);
                if (refCode == ResponseCode.LOGINFAILED1 || refCode == ResponseCode.LOGINFAILED2)
                {
                    return refCode;
                }
                else if (refCode != ResponseCode.GETETAG)
                {
                    return refCode;
                }
            }
            else
            {
                MarkParentsStatus(strPath, DB_STATUS_IN_PROGRESS);
                try
                {
                    Directory.CreateDirectory(strPath);
                }
                catch (System.IO.PathTooLongException ex)
                {
                    LogWrapper.LogMessage("frmSyncManager - nqEventCdmiCreate", "Caught exception: " + ex.Message);
                }

                strEtag = cMezeoFileCloud.GetETag(nsResult.StrContentsUri, ref refCode);
                if (refCode == ResponseCode.LOGINFAILED1 || refCode == ResponseCode.LOGINFAILED2)
                {
                    return refCode;
                }
                else if (refCode != ResponseCode.GETETAG)
                {
                    return refCode;
                }

                bRet = true;
            }

            if (bRet)
            {
                MarkParentsStatus(strPath, DB_STATUS_SUCCESS);
                if (nqDetail.StrObjectType == "DIRECTORY")
                {
                    DirectoryInfo dInfo = new DirectoryInfo(strPath);
                    dbHandler.UpdateModifiedDate(dInfo.LastWriteTime, fileFolderInfo.Key);
                    dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.E_TAG , strEtag , DbHandler.KEY , fileFolderInfo.Key );
                    dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.STATUS ,"SUCCESS", DbHandler.KEY ,fileFolderInfo.Key);
                }
                else
                {
                    FileInfo fInfo = new FileInfo(strPath);
                    dbHandler.UpdateModifiedDate(fInfo.LastWriteTime, fileFolderInfo.Key);
                    dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.E_TAG , strEtag , DbHandler.KEY , fileFolderInfo.Key );
                    dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.STATUS , "SUCCESS", DbHandler.KEY ,fileFolderInfo.Key );
                    nStatus = 1;
                }
            }

            if (nqDetail.StrObjectType == "DIRECTORY")
            {
                ItemDetails[] iDetails = cMezeoFileCloud.DownloadItemDetails(nsResult.StrContentsUri, ref nStatusCode, null);
                if (nStatusCode == ResponseCode.LOGINFAILED1 || nStatusCode == ResponseCode.LOGINFAILED2)
                {
                    return nStatusCode;
                }
                else if (nStatusCode == ResponseCode.INTERNAL_SERVER_ERROR)
                {
                    // Don't do anything, just keep on chugging.
                }
                else if (nStatusCode != ResponseCode.DOWNLOADITEMDETAILS)
                {
                    return nStatusCode;
                }
                if (iDetails != null)
                {
                    for (int num = 0; num < iDetails[0].nTotalItem; num++)
                    {
                        nStatus = DownloadFolderStructureForNQ(iDetails[num], strKey);
                        if (nStatus == ResponseCode.LOGINFAILED1 || nStatus == ResponseCode.LOGINFAILED2)
                        {
                            return nStatus;
                        }
                        else if (nStatus != ResponseCode.GETETAG && nStatus != ResponseCode.DOWNLOADFILE && nStatus != ResponseCode.DOWNLOADITEMDETAILS && nStatus != 1)
                        {
                            return nStatus;
                        }
                    }
                }
                else
                    nStatus = 1;
            }

            //LogWrapper.LogMessage("frmSyncManager - nqEventCdmiCreate", "leave");
            return nStatus;
        }
        private int DownloadFolderStructureForNQ(ItemDetails iDetail,string strParentKey)
        {
            //LogWrapper.LogMessage("frmSyncManager - DownloadFolderStructureForNQ", "enter");
            //bool bIssuccess = false;
            int nStatus = 0;
            FileFolderInfo fileFolderInfo = new FileFolderInfo();

            fileFolderInfo.IsPublic = iDetail.bPublic;
            fileFolderInfo.IsShared = iDetail.bShared;
            fileFolderInfo.ContentUrl = iDetail.szContentUrl;
            fileFolderInfo.CreatedDate = iDetail.dtCreated;
            fileFolderInfo.FileName = iDetail.strName;
            fileFolderInfo.FileSize = iDetail.dblSizeInBytes;
            fileFolderInfo.MimeType = iDetail.szMimeType;
            fileFolderInfo.ModifiedDate = iDetail.dtModified;
            fileFolderInfo.ParentUrl = iDetail.szParentUrl;
            fileFolderInfo.Status = DB_STATUS_IN_PROGRESS;
            fileFolderInfo.Type = iDetail.szItemType;
            fileFolderInfo.Key = strParentKey + "\\" + iDetail.strName;

            string strPath = BasicInfo.SyncDirPath + "\\" + fileFolderInfo.Key;

            int nStatusCode = 0;
            bool bRet = false;
            int lastSepIndex = fileFolderInfo.Key.LastIndexOf("\\");
            string parentDirPath = "";

            if (lastSepIndex != -1)
            {
                parentDirPath = fileFolderInfo.Key.Substring(0, fileFolderInfo.Key.LastIndexOf("\\"));
                parentDirPath = parentDirPath.Substring(parentDirPath.LastIndexOf("\\") + 1);
            }

            fileFolderInfo.ParentDir = parentDirPath;

            if (fileFolderInfo.ETag == null) { fileFolderInfo.ETag = ""; }
            if (fileFolderInfo.MimeType == null) { fileFolderInfo.MimeType = ""; }

            dbHandler.Write(fileFolderInfo);

            string strEtag = "";
            int refCode = 0;

            if (iDetail.szItemType == "FILE")
            {
                MarkParentsStatus(strPath, DB_STATUS_IN_PROGRESS);
                bRet = cMezeoFileCloud.DownloadFile(iDetail.szContentUrl + '/' + iDetail.strName, strPath,iDetail.dblSizeInBytes, ref nStatusCode);
                if (nStatusCode == ResponseCode.LOGINFAILED1 || nStatusCode == ResponseCode.LOGINFAILED2)
                {
                    return nStatusCode;
                }
                else if (nStatusCode != ResponseCode.DOWNLOADFILE)
                {
                    return nStatusCode;
                }
                if (bRet)
                {
                    strEtag = cMezeoFileCloud.GetETag(iDetail.szContentUrl, ref refCode);
                    if (refCode == ResponseCode.LOGINFAILED1 || refCode == ResponseCode.LOGINFAILED2)
                    {
                        return refCode;
                    }
                    else if (refCode != ResponseCode.GETETAG)
                    {
                        return refCode;
                    }
                    FileInfo fInfo = new FileInfo(strPath);
                    dbHandler.UpdateModifiedDate(fInfo.LastWriteTime, fileFolderInfo.Key);
                    dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.E_TAG , strEtag , DbHandler.KEY , fileFolderInfo.Key );
                    dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.STATUS , "SUCCESS", DbHandler.KEY ,fileFolderInfo.Key );
                }
                MarkParentsStatus(strPath, DB_STATUS_SUCCESS);
                nStatus = 1;
            }
            else
            {
                MarkParentsStatus(strPath, DB_STATUS_IN_PROGRESS);
                Directory.CreateDirectory(strPath);

                strEtag = cMezeoFileCloud.GetETag(iDetail.szContentUrl, ref refCode);
                if (refCode == ResponseCode.LOGINFAILED1 || refCode == ResponseCode.LOGINFAILED2)
                {
                    return refCode;
                }
                else if (refCode != ResponseCode.GETETAG)
                {
                    return refCode;
                }

                DirectoryInfo dInfo = new DirectoryInfo(strPath);
                dbHandler.UpdateModifiedDate(dInfo.LastWriteTime, fileFolderInfo.Key);
                dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.E_TAG , strEtag , DbHandler.KEY , fileFolderInfo.Key );
                dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.STATUS , "SUCCESS", DbHandler.KEY ,fileFolderInfo.Key );

                MarkParentsStatus(strPath, DB_STATUS_SUCCESS);

                ItemDetails[] iDetails = cMezeoFileCloud.DownloadItemDetails(iDetail.szContentUrl, ref nStatusCode, null);
                if (nStatusCode == ResponseCode.LOGINFAILED1 || nStatusCode == ResponseCode.LOGINFAILED2)
                {
                    return nStatusCode;
                }
                else if (nStatusCode == ResponseCode.INTERNAL_SERVER_ERROR)
                {
                    // Don't do anything, just keep on chugging.
                }
                else if (nStatusCode != ResponseCode.DOWNLOADITEMDETAILS)
                {
                    return nStatusCode;
                }
                if (iDetails != null)
                {
                    for (int num = 0; num < iDetails[0].nTotalItem; num++)
                    {
                        nStatus = DownloadFolderStructureForNQ(iDetails[num], fileFolderInfo.Key);
                        if (nStatus == ResponseCode.LOGINFAILED1 || nStatus == ResponseCode.LOGINFAILED2)
                        {
                            return nStatus;
                        }
                        else if (nStatus != ResponseCode.GETETAG && nStatus != ResponseCode.DOWNLOADFILE && nStatus != ResponseCode.DOWNLOADITEMDETAILS)
                        {
                            return nStatus;
                        }
                    }
                }
                else
                    nStatus = 1;
            }

            //LogWrapper.LogMessage("frmSyncManager - DownloadFolderStructureForNQ", "leave");
            return nStatus;
        }
        private int ConsumeLocalItemDetail(LocalItemDetails itemDetail)
        {
            int nResultStatus = 1;
            LogWrapper.LogMessage("frmSyncManager - consume", "Enter");
            ItemDetails id = itemDetail.ItemDetails;
            LogWrapper.LogMessage("frmSyncManager - consume", "creating file folder info for " + id.strName);

            using (FileFolderInfo fileFolderInfo = new FileFolderInfo())
            {
                fileFolderInfo.Key = itemDetail.Path;

                fileFolderInfo.IsPublic = id.bPublic;
                fileFolderInfo.IsShared = id.bShared;
                fileFolderInfo.ContentUrl = id.szContentUrl;
                fileFolderInfo.CreatedDate = id.dtCreated;
                fileFolderInfo.FileName = id.strName;
                fileFolderInfo.FileSize = id.dblSizeInBytes;
                fileFolderInfo.MimeType = id.szMimeType;
                fileFolderInfo.ModifiedDate = id.dtModified;
                fileFolderInfo.ParentUrl = id.szParentUrl;
                fileFolderInfo.Status = "INPROGRESS";
                fileFolderInfo.Type = id.szItemType;

                int lastSepIndex = itemDetail.Path.LastIndexOf("\\");
                string parentDirPath = "";

                if (lastSepIndex != -1)
                {
                    parentDirPath = itemDetail.Path.Substring(0, itemDetail.Path.LastIndexOf("\\"));
                    parentDirPath = parentDirPath.Substring(parentDirPath.LastIndexOf("\\") + 1);
                }
                //else
                //{
                //    parentDirPath = itemDetail.Path;
                //}

                fileFolderInfo.ParentDir = parentDirPath;

                if (fileFolderInfo.ETag == null)
                {
                    fileFolderInfo.ETag = "";
                }
                if (fileFolderInfo.MimeType == null)
                {
                    fileFolderInfo.MimeType = "";
                }

                LogWrapper.LogMessage("frmSyncManager - consume", "writing file folder info for " + id.strName + " in DB");
                //if (!dbHandler.Write(fileFolderInfo))
                //    return 1;
                dbHandler.Write(fileFolderInfo);

                string downloadObjectName = BasicInfo.SyncDirPath + "\\" + itemDetail.Path;

                LogWrapper.LogMessage("frmSyncManager - consume", "download object " + downloadObjectName);

                LogWrapper.LogMessage("frmSyncManager - consume", "setting parent folders status DB_STATUS_IN_PROGRESS, bRet FALSE");
                MarkParentsStatus(downloadObjectName, DB_STATUS_IN_PROGRESS);
                bool bRet = false;
                int refCode = 0;

                if (id.szItemType == "DIRECTORY")
                {
                    LogWrapper.LogMessage("frmSyncManager - consume", id.strName + " is DIRECTORY");
                    System.IO.Directory.CreateDirectory(downloadObjectName);

                    if (id.strETag.Trim().Length == 0)
                    {
                        LogWrapper.LogMessage("frmSyncManager - consume", "Getting eTag for " + id.strName);
                        id.strETag = cMezeoFileCloud.GetETag(id.szContentUrl, ref refCode);
                        if (refCode == ResponseCode.LOGINFAILED1 || refCode == ResponseCode.LOGINFAILED2)
                        {
                            lockObject.StopThread = true;
                            return ResponseCode.LOGINFAILED1;  // CancelReason.LOGINFAILED
                        }
                        else if (refCode != ResponseCode.GETETAG)
                        {
                            lockObject.StopThread = true;
                            if (ResponseCode.NOTFOUND == refCode)
                                return ResponseCode.NOTFOUND;
                            return ResponseCode.SERVER_INACCESSIBLE; // CancelReason.SERVER_INACCESSIBLE
                        }
                    }

                    LogWrapper.LogMessage("frmSyncManager - consume", "eTag for " + id.strName + ": " + id.strETag + ", bRet TRUE");
                    bRet = true;
                }
                else
                {
                    LogWrapper.LogMessage("frmSyncManager - consume", id.strName + " is NOT DIRECTORY");
                    bRet = cMezeoFileCloud.DownloadFile(id.szContentUrl + '/' + id.strName,
                                            downloadObjectName, id.dblSizeInBytes, ref refCode);

                    if (refCode == ResponseCode.LOGINFAILED1 || refCode == ResponseCode.LOGINFAILED2)
                    {
                        //lockObject.StopThread = true;
                        return ResponseCode.LOGINFAILED1; // CancelReason.LOGIN_FAILED
                    }
                    else if (refCode == ResponseCode.NOTFOUND)
                    {
                        //lockObject.StopThread = true;
                        return ResponseCode.NOTFOUND;
                    }
                    else if (refCode != ResponseCode.DOWNLOADFILE)
                    {
                        //lockObject.StopThread = true;
                        return ResponseCode.SERVER_INACCESSIBLE; // CancelReason.SERVER_INACCESSIBLE
                    }

                    LogWrapper.LogMessage("frmSyncManager - consume", "bRet for " + id.strName + " is " + bRet.ToString());
                    if (refCode == ResponseCode.INSUFFICIENT_STORAGE_AVAILABLE)
                    {
                        LogWrapper.LogMessage("frmSyncManager - consume", "INSUFFICIENT_STORAGE_AVAILABLE, calling CancelAndNotify with reason INSUFFICIENT_STORAGE");
                        return ResponseCode.INSUFFICIENT_STORAGE_AVAILABLE;
                    }

                    LogWrapper.LogMessage("frmSyncManager - consume", "Getting eTag for " + id.strName);
                    id.strETag = cMezeoFileCloud.GetETag(id.szContentUrl, ref refCode);

                    if (refCode == ResponseCode.LOGINFAILED1 || refCode == ResponseCode.LOGINFAILED2)
                    {
                        //lockObject.StopThread = true;
                        return ResponseCode.LOGINFAILED1; // CancelReason.LOGIN_FAILED
                    }
                    else if (refCode != ResponseCode.GETETAG)
                    {
                        //lockObject.StopThread = true;
                        if (refCode == ResponseCode.NOTFOUND)
                            return ResponseCode.NOTFOUND;
                        return ResponseCode.SERVER_INACCESSIBLE; // CancelReason.SERVER_INACCESSIBLE
                    }
                    LogWrapper.LogMessage("frmSyncManager - consume", "eTag for " + id.strName + ": " + id.strETag);
                }

                if (!bRet)
                {
                    LogWrapper.LogMessage("frmSyncManager - consume", "bRet FALSE, writing to cFileCloud.AppEventViewer");
                    string Description = "";
                    Description += LanguageTranslator.GetValue("ErrorBlurbDownload1");
                    Description += LanguageTranslator.GetValue("ErrorBlurbDownload2");
                    Description += LanguageTranslator.GetValue("ErrorBlurbDownload3");
                    // cFileCloud.AppEventViewer(AboutBox.AssemblyTitle, Description, 3);
                }
                else
                {
                    LogWrapper.LogMessage("frmSyncManager - consume", "setting parent folders status to DB_STATUS_SUCCESS for " + downloadObjectName);
                    MarkParentsStatus(downloadObjectName, DB_STATUS_SUCCESS);
                    //fileFolderInfo.ETag = id.strETag;
                    if (id.szItemType == "DIRECTORY")
                    {
                        LogWrapper.LogMessage("frmSyncManager - consume", "updating DB for folder " + downloadObjectName);
                        DirectoryInfo dInfo = new DirectoryInfo(downloadObjectName);
                        dbHandler.UpdateModifiedDate(dInfo.LastWriteTime, fileFolderInfo.Key);
                        dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.E_TAG, id.strETag, DbHandler.KEY, fileFolderInfo.Key);
                        dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.STATUS, "SUCCESS", DbHandler.KEY, fileFolderInfo.Key);
                    }
                    else
                    {
                        LogWrapper.LogMessage("frmSyncManager - consume", "updating DB for file " + downloadObjectName);
                        FileInfo fInfo = new FileInfo(downloadObjectName);
                        dbHandler.UpdateModifiedDate(fInfo.LastWriteTime, fileFolderInfo.Key);
                        dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.E_TAG, id.strETag, DbHandler.KEY, fileFolderInfo.Key);
                        dbHandler.Update(DbHandler.TABLE_NAME, DbHandler.STATUS, "SUCCESS", DbHandler.KEY, fileFolderInfo.Key);
                    }

                    //if (downloadEvent != null)
                    //{
                    //    LogWrapper.LogMessage("frmSyncManager - consume", "calling  downloadEvent with " + downloadObjectName);
                    //    downloadEvent(this, new FileDownloaderEvents(downloadObjectName, 0));
                    //}
                }
            }

            //if (IsAnalysisCompleted && queue.Count == 0)
            //{
            //    LogWrapper.LogMessage("FileDownloader - consume", "Analysis completed and queue lenth is ZERO");
            //    if (fileDownloadCompletedEvent != null)
            //    {
            //        LogWrapper.LogMessage("FileDownloader - consume", "calling fileDownloadCompletedEvent");
            //        done = true;
            //        fileDownloadCompletedEvent();
            //    }
            //}

            LogWrapper.LogMessage("frmSyncManager - consume", "Leave");
            return nResultStatus;
        }
        private void AddInDBForAdded(LocalEvents lEvent)
        {
            FileFolderInfo fInfo = new FileFolderInfo();

            FileInfo fileInfo = new FileInfo(lEvent.FullPath);

            fInfo.Key = lEvent.FileName;
            fInfo.ContentUrl = "";
            fInfo.ParentUrl = GetParentURI(lEvent.FileName);
            fInfo.CreatedDate = fileInfo.CreationTime;
            fInfo.ModifiedDate = fileInfo.LastWriteTime;
            fInfo.MimeType = "";
            fInfo.IsPublic = false;
            fInfo.IsShared = false;
            fInfo.Status = DB_STATUS_IN_PROGRESS;

            fInfo.ETag = "";

            if (lEvent.FileName.LastIndexOf("\\") == -1)
            {
                fInfo.FileName = lEvent.FileName;
                fInfo.ParentDir = "";
            }
            else
            {
                fInfo.FileName = lEvent.FileName.Substring(lEvent.FileName.LastIndexOf("\\") + 1);
                fInfo.ParentDir = lEvent.FileName.Substring(0, lEvent.FileName.LastIndexOf("\\"));
                fInfo.ParentDir = fInfo.ParentDir.Substring(fInfo.ParentDir.LastIndexOf("\\") +1);
            }

            if ((fileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
            {
                fInfo.Type = "DIRECTORY";
                fInfo.FileSize = 0;
            }
            else
            {
                fInfo.Type = "FILE";
                fInfo.FileSize = fileInfo.Length;
            }

            dbHandler.Write(fInfo);
        }