Пример #1
0
ChmFile *
chmfile_new(const gchar *filename)
{
  ChmFile *chmfile;
  gchar *bookmark_file;
  gchar *md5;

  /* Use chmfile MD5 as book folder name */
  md5 = MD5File(filename, NULL);
  if(!md5) {
    return NULL;
  }

  chmfile = g_object_new(TYPE_CHMFILE, NULL);


  chmfile->dir = g_build_filename(g_getenv("HOME"),
                                  ".chmsee",
                                  "bookshelf",
                                  md5,
                                  NULL);
  g_debug("book dir = %s", chmfile->dir);

  /* If this chm file extracted before, load it's bookinfo */
  if (!g_file_test(chmfile->dir, G_FILE_TEST_IS_DIR)) {
    if (!extract_chm(filename, chmfile)) {
      g_debug("extract_chm failed: %s", filename);
      return NULL;
    }

    chmfile->filename = g_strdup(filename);
    g_debug("chmfile->filename = %s", chmfile->filename);

    chmfile_file_info(chmfile);
    save_fileinfo(chmfile);
  } else {
    load_fileinfo(chmfile);
  }

  g_debug("chmfile->hhc = %s", chmfile->hhc);
  g_debug("chmfile->hhk = %s", chmfile->hhk);
  g_debug("chmfile->home = %s", chmfile->home);
  g_debug("chmfile->title = %s", chmfile->title);
  g_debug("chmfile->endcoding = %s", chmfile->encoding);

  /* Parse hhc and store result to tree view */
  if (chmfile->hhc != NULL && g_ascii_strcasecmp(chmfile->hhc, "(null)") != 0) {
    gchar *hhc;

    hhc = g_strdup_printf("%s%s", chmfile->dir, chmfile->hhc);

    if (g_file_test(hhc, G_FILE_TEST_EXISTS)) {
      chmfile->link_tree = hhc_load(hhc, chmfile->encoding);
    } else {
      gchar *hhc_ncase;

      hhc_ncase = file_exist_ncase(hhc);
      chmfile->link_tree = hhc_load(hhc_ncase, chmfile->encoding);
      g_free(hhc_ncase);
    }

    g_free(hhc);
  } else {
    g_message(_("Can't found hhc file."));
  }

  /* Load bookmarks */
  bookmark_file = g_build_filename(chmfile->dir, CHMSEE_BOOKMARK_FILE, NULL);
  chmfile->bookmarks_list = bookmarks_load(bookmark_file);
  g_free(bookmark_file);

  return chmfile;
}
Пример #2
0
bool upload_file(char *path,char * filename,nsp_node &info,nsp_node &recv,bool simpletype=false,void *callfunc=NULL){

    nsplog("上传文件",filename);
    char cfile[MAX_PATH];
    char appid[33];
    char server[32];
    char tstr[32];
    char secret[33];
    nsp_node *nsp_search;

    nspAction *nA=NSPAction();
    if(nA==NULL){return false;}

    nsp_file_info file_info; 
	memset(file_info.filename,0,sizeof(file_info.filename));
	file_info.nsi.next=NULL;

    int nspfsize = -1;
    //upauth information
    nsp_search = nA->searchNodeByStr(&info,"nsp_tapp");
    if(nsp_search!=NULL){strcpy(appid,nsp_search->node_data.valuestring);}else{nsplog("内部错误","文件上传鉴权出错");goto upload_fail;}
    nsp_search = nA->searchNodeByStr(&info,"nsp_host");
    if(nsp_search!=NULL){strcpy(server,nsp_search->node_data.valuestring);}else{nsplog("内部错误","文件上传鉴权出错");goto upload_fail;}
    nsp_search = nA->searchNodeByStr(&info,"nsp_tstr");
    if(nsp_search!=NULL){sprintf(tstr,"%d",nsp_search->node_data.valueint);}else{nsplog("内部错误","文件上传鉴权出错");goto upload_fail;}
    nsp_search = nA->searchNodeByStr(&info,"secret");
    if(nsp_search!=NULL){strcpy(secret,nsp_search->node_data.valuestring);}else{nsplog("内部错误","文件上传鉴权出错");goto upload_fail;}

    strcpy(cfile, filename);

    nspfsize = get_file_size(cfile); //get length of file
    if(nspfsize == -1){
        nsplog("内部错误","获取上传文件大小出错");
        goto upload_fail;
    }
    if(nspfsize>BIGFILESIZE){
        file_info.isbigfile=true;
    }
    else{file_info.isbigfile=false;}
    // store file(>100M) seginfo 

    // control big file size
    if(!abletoUpload(nspfsize)){
        nsplog("内部错误","网盘空间已满或者无权限上传大文件");
        goto upload_fail;
    }

    if(!load_fileinfo(file_info,filename)){
        nsplog("内部错误","获取/计算文件信息出错");
        goto upload_fail;
    }
    sent_bytes = 0;


    nsp_ret_t nsp_ret;
    if(!up_init(appid,server, tstr, secret, cfile, nspfsize, &nsp_ret ,file_info.pfileid )){
        nsplog("内部错误","上传初始化失败");
        goto upload_fail;
    }
    while(1){
        switch (nsp_ret.nsp_upcmd){
        case REQTCON:
            if(!up_retcon(appid,server, tstr, secret, cfile, nspfsize, &nsp_ret, &file_info)){
                nsplog("内部错误","飞速上传出错");
                goto upload_fail;
            }
            break;
        case REQTBLK:
            if(!up_blk(appid,server, tstr, secret, cfile, nspfsize, &nsp_ret, &file_info, callfunc)){
                nsplog("内部错误","文件分块上传出错");
                goto upload_fail;
            }
            break;
        case UPSTAT:
            if(nsp_ret.nsp_upstat == NONE){
                nsplog("内部错误","未知的上传状态");
                goto upload_fail;
            }else if(nsp_ret.nsp_upstat == FIN){
                //begin to mkfile
                nA->appendMap_Str_Str(&recv,"nsp_fid",nsp_ret.nsp_fid);
                nA->appendMap_Str_Str(&recv,"nsp_path",nsp_ret.nsp_path);
                nA->appendMap_Str_Str(&recv,"nsp_size",nsp_ret.nsp_size);
                nA->appendMap_Str_Str(&recv,"nsp_sig",nsp_ret.nsp_sig);
                nA->appendMap_Str_Int(&recv,"nsp_ts",nsp_ret.nsp_ts);
                NSPActionDelete(nA);
                remove_cache(file_info.filename);
                if(file_info.isbigfile && file_info.nsi.next!=NULL){
                    nsp_bigfile_clean(file_info.nsi.next);
                }
                if(callfunc!=NULL){
                    callupload_func = (void (*)(double,double))callfunc;
                    callupload_func((double)nspfsize,(double)nspfsize);
                }
                return true; 
            }
            nsplog("内部错误","未知的上传状态");
            goto upload_fail;
        case REQTSEG:
            if(!up_seginfo(appid,server, tstr, secret, cfile, nspfsize, &nsp_ret ,&file_info)){
                nsplog("内部错误","文件分块信息出错");
                goto upload_fail;
            }
            file_info.isbigfile=true;
            break;
        default:
            nsplog("内部错误","未知的上传状态");
            goto upload_fail;
        }
    }
    return true;

upload_fail:
    NSPActionDelete(nA);
    remove_cache(file_info.filename);
    if(file_info.isbigfile && file_info.nsi.next!=NULL){
        nsp_bigfile_clean(file_info.nsi.next);
    }
    return false;
}