Пример #1
0
static STATUS _file_scan(char *path, int path_len)
{
    return_val_if_fail(path != NULL, RET_FAIL);
    return_val_if_fail(strlen(path) <= path_len, RET_INVALID_ARG);
    DIR* d = NULL;
    struct dirent* de = NULL;
    int i = 0;
    int result = 0;
    d = opendir(path);
    return_val_if_fail(d != NULL, RET_FAIL);

    int d_size = 0;
    int d_alloc = 10;
    char** dirs = (char **)malloc(d_alloc * sizeof(char*));
    char** dirs_desc = (char **)malloc(d_alloc * sizeof(char*));
    return_val_if_fail(dirs != NULL, RET_FAIL);
    return_val_if_fail(dirs_desc != NULL, RET_FAIL);
    int z_size = 0;
    int z_alloc = 10;
    char** zips = (char **)malloc(z_alloc * sizeof(char*));
    char** zips_desc=(char **)malloc(z_alloc * sizeof(char*));
    return_val_if_fail(zips != NULL, RET_FAIL);
    return_val_if_fail(zips_desc != NULL, RET_FAIL);
//    zips[0] = strdup("../");
//    zips_desc[0]=strdup("../");

    while ((de = readdir(d)) != NULL) {
        int name_len = strlen(de->d_name);
        if (name_len <= 0) continue;
        char de_path[PATH_MAX];
        snprintf(de_path, PATH_MAX, "%s/%s", path, de->d_name);
        struct stat st ;
        assert_if_fail(stat(de_path, &st) == 0);
        if (de->d_type == DT_DIR) {
            //skip "." and ".." entries
            if (name_len == 1 && de->d_name[0] == '.') continue;
            if (name_len == 2 && de->d_name[0] == '.' && 
                    de->d_name[1] == '.') continue;
            if (d_size >= d_alloc) {
                d_alloc *= 2;
                dirs = (char **)realloc(dirs, d_alloc * sizeof(char*));
                assert_if_fail(dirs != NULL);
                dirs_desc = (char **)realloc(dirs_desc, d_alloc * sizeof(char*));
                assert_if_fail(dirs_desc != NULL);
            }
            dirs[d_size] = (char *)malloc(name_len + 2);
            assert_if_fail(dirs[d_size] != NULL);
            dirs_desc[d_size] = (char *)malloc(64);
            assert_if_fail(dirs_desc[d_size] != NULL);
            strncpy(dirs[d_size], de->d_name, name_len);
            dirs[d_size][name_len] = '/';
            dirs[d_size][name_len + 1] = '\0';
            snprintf(dirs_desc[d_size], 63, "%s" ,ctime(&st.st_mtime));
            dirs_desc[d_size][63] = '\0';
            ++d_size;
        } else if (de->d_type == DT_REG) {
            if (g_file_filter_fun == NULL || g_file_filter_fun(de->d_name, name_len) == 0)
            {
                if (z_size >= z_alloc) {
                    z_alloc *= 2;
                    zips = (char **) realloc(zips, z_alloc * sizeof(char*));
                    assert_if_fail(zips != NULL);
                    zips_desc = (char **) realloc(zips_desc, z_alloc * sizeof(char*));
                    assert_if_fail(zips_desc != NULL);
                }
                zips[z_size] = strdup(de->d_name);
                assert_if_fail(zips[z_size] != NULL);
                zips_desc[z_size] = (char*)malloc(64);
                assert_if_fail(zips_desc[z_size] != NULL);
                snprintf(zips_desc[z_size], 63, "%s   %lldbytes" ,ctime(&st.st_mtime), st.st_size);
                zips_desc[z_size][63] = '\0';
                z_size++;
            }
        }
    }
    closedir(d);


    // append dirs to the zips list
    if (d_size + z_size + 1 > z_alloc) {
        z_alloc = d_size + z_size + 1;
        zips = (char **)realloc(zips, z_alloc * sizeof(char*));
        assert_if_fail(zips != NULL);
        zips_desc = (char **)realloc(zips_desc, z_alloc * sizeof(char*));
        assert_if_fail(zips_desc != NULL);
    }
    for (i = 0; i < d_size; i++)
    {
        zips[z_size + i] = dirs[i];
        zips_desc[z_size + i] = dirs_desc[i];
    }
    free(dirs);
    z_size += d_size;
    zips[z_size] = NULL;
    zips_desc[z_size] = NULL;

	int chosen_item = 0;
	do {
      
		if (NULL == g_title_name) 
		{
			oppo_error("g_title_name is NULL \n");
			result = -1;
			goto finish_done;
		}
#if DEBUG
		sd_file_dump_array(zips, zips_desc, z_size);
#endif
		chosen_item = oppo_sdmenu(g_title_name, zips, zips_desc, z_size);

/* OPPO 2013-02-18 jizhengkang azx Add begin for reason */
		selectedItem[selectedCount] = chosen_item;

/* OPPO 2013-02-23 jizhengkang azx Modify begin for reason */
#if 0
		if (chosen_item == -1) {//huanggd for exit explorer when select "return" option
			result = 1;
			if (result_inter_sd || result_external_sd) {
				selectedCount = 1;
				memset(selectedItem + 2, 0, 1022);
			} else {
				selectedCount = 2;
				memset(selectedItem + 3, 0, 1021);
			}
			break;
		}
#endif
/* OPPO 2013-02-23 jizhengkang azx Modify end */

		if (chosen_item == -1) {
			result = -1;
			memset(selectedItem + selectedCount, 0, 1024 - selectedCount - 1);
			selectedCount--;
			break;
		}
		
		return_val_if_fail(chosen_item >= 0, RET_FAIL);
		char * item = zips[chosen_item];
		return_val_if_fail(item != NULL, RET_FAIL);
		int item_len = strlen(item);

//		((chosen_item > 0)&&(item[item_len - 1] == '/'))?selectedCount++:selectedCount--;
//		(chosen_item == 0)?selectedCount--:((item[item_len - 1] == '/')?selectedCount++:NULL);


/* OPPO 2013-02-18 jizhengkang azx Add end */




/* OPPO 2013-02-23 jizhengkang azx Delete begin for reason */
#if 0
		if ( chosen_item == 0) {
           //go up but continue browsing
			result = -1;
			break;
		} else 
#endif
/* OPPO 2013-02-23 jizhengkang azx Delete end */
		if (item[item_len - 1] == '/') {
			selectedCount++;
			char new_path[PATH_MAX];
			strlcpy(new_path, path, PATH_MAX);
			strlcat(new_path, "/", PATH_MAX);
			strlcat(new_path, item, PATH_MAX);
			new_path[strlen(new_path) - 1] = '\0';
			result = _file_scan(new_path, PATH_MAX);
			if (result > 0) break;
		} else {
           // select a zipfile
           // the status to the caller
			char new_path[PATH_MAX];
			strlcpy(new_path, path, PATH_MAX);
			strlcat(new_path, "/", PATH_MAX);
			strlcat(new_path, item, PATH_MAX);
			int wipe_cache = 0;
			//if third parameter is 1, echo sucess dialog
			if (NULL == g_fun) 
			{
				oppo_error("g_fun is NULL in fun\n");
				result = -1;
				goto finish_done;
			}

			if (oppo_get_battery_capacity() < 15) {
				if (!oppo_is_ac_usb_online()) {
					oppo_notice(3, "<~sd.install.notice>", "<~sd.install.caplow.desc>", "@sd");
					continue;			
				}
	    	}

		    if (canvas_sdfile_inited == 0) {
				canvas_sdfile_inited = 1;
				ag_canvas(&canvas_sdfile,agw(),agh());	
		    }
		    ag_draw(&canvas_sdfile,agc(),0,0);  

		    int ret = g_fun(new_path, PATH_MAX, (void *)g_data);
			if (0 == ret)//execute callback fun success
			{
				//back to up layer
				//result = -1;//huanggd for exit explorer when install successfully
				selectedCount = 1;
				memset(selectedItem + 2, 0, 1022);
				result = 1;
				feed_back("2");
			}
			else if(1 == ret){
				continue;//cacel install
			} else {
				oppo_error("g_fun execute fail\n");
				result = 0;
				feed_back("3");

				ag_draw(NULL,&canvas_sdfile,0,0);
				oppo_notice(3, "<~sd.install.failed.name>", "<~sd.install.failed.desc>", "@sd");
				continue;   
			}
			break;
		}
	} while(1);

finish_done:

	for (i = 0; i < z_size; ++i)
	{
		free(zips[i]);
		free(zips_desc[i]);
	}
	free(zips);
	return result;
}
Пример #2
0
int32 DCCReceive::Transfer(void* arg)
{
	BMessenger msgr(reinterpret_cast<DCCReceive*>(arg));
	struct sockaddr_in address;
	BLooper* looper(NULL);

	int32 dccSock(-1);

	if ((dccSock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		UpdateStatus(msgr, S_DCC_ESTABLISH_ERROR);
		return B_ERROR;
	}

	BMessage reply;

	if (msgr.SendMessage(M_DCC_GET_CONNECT_DATA, &reply) == B_ERROR) return B_ERROR;

	memset(&address, 0, sizeof(sockaddr_in));
	address.sin_family = AF_INET;
	address.sin_port = htons(atoi(reply.FindString("port")));
	address.sin_addr.s_addr = htonl(strtoul(reply.FindString("ip"), 0, 10));

	UpdateStatus(msgr, S_DCC_CONNECT_TO_SENDER);
	if (connect(dccSock, (sockaddr*)&address, sizeof(address)) < 0) {
		UpdateStatus(msgr, S_DCC_ESTABLISH_ERROR);
		close(dccSock);
		return B_ERROR;
	}

	BPath path(reply.FindString("name"));
	BString buffer;
	off_t file_size(0);

	buffer << S_DCC_RECV1 << path.Leaf() << S_DCC_RECV2 << reply.FindString("nick") << ".";

	UpdateStatus(msgr, buffer.String());

	BFile file;

	if (msgr.IsValid()) {
		if (reply.FindBool("resume")) {
			if (file.SetTo(reply.FindString("name"), B_WRITE_ONLY | B_OPEN_AT_END) == B_NO_ERROR &&
				file.GetSize(&file_size) == B_NO_ERROR && file_size > 0LL)
				UpdateBar(msgr, file_size, 0, 0, true);
			else
				file_size = 0LL;
		} else {
			file.SetTo(reply.FindString("name"), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
		}
	}

	uint32 bytes_received(file_size);
	uint32 size(atol(reply.FindString("size")));
	uint32 cps(0);

	if (file.InitCheck() == B_NO_ERROR) {
		bigtime_t last(system_time()), now;
		char inBuffer[8196];
		bigtime_t start = system_time();

		while ((msgr.Target(&looper) != NULL) && bytes_received < size) {
			int readSize;
			if ((readSize = recv(dccSock, inBuffer, 8196, 0)) < 0) break;

			file.Write(inBuffer, readSize);
			bytes_received += readSize;
			BMessage msg(M_DCC_UPDATE_TRANSFERRED);
			msg.AddInt32("transferred", bytes_received);
			msgr.SendMessage(&msg);

			uint32 feed_back(htonl(bytes_received));
			send(dccSock, &feed_back, sizeof(uint32), 0);

			now = system_time();
			bool hit(false);

			if (now - last > 500000) {
				cps = (int)ceil((bytes_received - file_size) / ((now - start) / 1000000.0));
				BMessage updmsg(M_DCC_UPDATE_AVERAGE);
				updmsg.AddInt32("average", cps);
				msgr.SendMessage(&updmsg);
				last = now;
				hit = true;
			}

			DCCConnect::UpdateBar(msgr, readSize, cps, bytes_received, hit);
		}
	}

	if (msgr.IsValid()) {
		BMessage msg(M_DCC_STOP_BUTTON);
		msgr.SendMessage(&msg);
	}

	if (dccSock > 0) {
		close(dccSock);
	}

	if (file.InitCheck() == B_OK) {
		file.Unset();
		update_mime_info(reply.FindString("name"), 0, 0, 1);
	}
	return 0;
}