Ejemplo n.º 1
0
BOOL VcardForm::savePhoto( LPCTSTR path ) 
{
    if (!vcard) return FALSE;

    JabberDataBlockRef vcardTemp=vcard->findChildNamespace("vCard", "vcard-temp");      if (!vcardTemp) return FALSE;
    JabberDataBlockRef photo=vcardTemp->getChildByName("PHOTO");    if (!photo) return FALSE;
    JabberDataBlockRef binval=photo->getChildByName("BINVAL"); if (!binval) return FALSE;
    const std::string &data=binval->getText();

    int dstLen=base64::base64DecodeGetLength(data.length());
    char *dst=new char[dstLen];

    dstLen=base64::base64Decode2(dst, data.c_str(), data.length());

    wchar_t filename[MAX_PATH];
    if (!path) {
        wchar_t *extm=ext[detectMime(dst)]; 
        *filename=0;
        wcscpy_s(filename, MAX_PATH, extm);
        OPENFILENAME ofn;
        memset(&ofn, 0, sizeof(OPENFILENAME));
        ofn.lStructSize=sizeof(OPENFILENAME);
        ofn.hwndOwner=getHWnd();         
        ofn.hInstance=g_hInst; //GetModuleHandle(NULL);  
        ofn.lpstrFilter=FILEIMGMASK;     
        ofn.nFilterIndex=0;
        ofn.lpstrFile=filename;     
        ofn.nMaxFile=MAX_PATH;
        //ofn.lpstrInitialDir   = ;     
        ofn.lpstrTitle=L"Save image as";
        ofn.lpstrDefExt=extm+2;
        ofn.Flags=OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;

        BOOL result = GetSaveFileNameEx(&ofn);
        if (!result) return FALSE;
        path=filename;
    }


    HANDLE file=CreateFile(path, 
        GENERIC_WRITE, 
        FILE_SHARE_READ, NULL, 
        CREATE_ALWAYS,
        0, NULL);

    DWORD dwProcessed;
    if (file==INVALID_HANDLE_VALUE) {
        delete dst; return FALSE;
    }
    WriteFile(file, dst, dstLen, &dwProcessed, NULL);
    CloseHandle(file);

    delete dst;
    return TRUE;
}
Ejemplo n.º 2
0
BOOL VcardForm::savePhoto( LPCTSTR path ) 
{
    if (!vcard) return FALSE;

    JabberDataBlockRef vcardTemp=vcard->findChildNamespace("vCard", "vcard-temp");      if (!vcardTemp) return FALSE;
    JabberDataBlockRef photo=vcardTemp->getChildByName("PHOTO");    if (!photo) return FALSE;
    JabberDataBlockRef binval=photo->getChildByName("BINVAL"); if (!binval) return FALSE;
    const std::string &data=binval->getText();

    int dstLen=base64::base64DecodeGetLength(data.length());
    char *dst=new char[dstLen];

    dstLen=base64::base64Decode2(dst, data.c_str(), data.length());

    wchar_t filename[MAX_PATH];
    if (!path) {
        wchar_t *extm=ext[detectMime(dst)]; 
        *filename=0;
        wcscpy_s(filename, MAX_PATH, extm);
        OPENFILENAME ofn;
        memset(&ofn, 0, sizeof(OPENFILENAME));
        ofn.lStructSize=sizeof(OPENFILENAME);
        ofn.hwndOwner=getHWnd();         
        ofn.hInstance=g_hInst; //GetModuleHandle(NULL);  
        ofn.lpstrFilter=FILEIMGMASK;     
        ofn.nFilterIndex=0;
        ofn.lpstrFile=filename;     
        ofn.nMaxFile=MAX_PATH;
        //ofn.lpstrInitialDir   = ;     
        ofn.lpstrTitle=L"Save image as";
        ofn.lpstrDefExt=extm+2;
        ofn.Flags=OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;

        BOOL result = GetSaveFileNameEx(&ofn);
        if (!result) return FALSE;
        path=filename;
    }

	std::string vcard_to = vcard->getAttribute("from");
size_t i=0;
                while (i<vcard_to.length()) {
                    if (vcard_to[i]=='/') {
                       vcard_to[i]='.';
                        continue;
                    }
                    i++;
				}
	//Log::getInstance()->msg("[***]CASHED Image OK("+vcard_from.substr(0,vcard_from.find("/",0))+")");
	std::wstring new_path = appRootPath + L"userdata\\avatars\\" + utf8::utf8_wchar(vcard_to) + L".jpg";
    std::wstring imageFile=appRootPath + L"userdata\\avatars\\" + utf8::utf8_wchar(vcard_to) + L".jpg";
	//int result=MessageBox(NULL, new_path.c_str(), TEXT("123"), MB_YESNO | MB_ICONWARNING );
	LPCTSTR path_save=imageFile.c_str();
	Log::getInstance()->msg("[***Get.]("+vcard_to+")");

    HANDLE file=CreateFile(path_save,
        GENERIC_WRITE, 
        FILE_SHARE_READ, NULL, 
        CREATE_ALWAYS,
        0, NULL);

    DWORD dwProcessed;
    if (file==INVALID_HANDLE_VALUE) {
        delete dst; return FALSE;
    }
    WriteFile(file, dst, dstLen, &dwProcessed, NULL);
    CloseHandle(file);


	//создание файла
	Contact::ref contact;
	 contact=rc.lock()->roster->getContactEntry(vcard_to);
	 contact->img_avatar.reset();
	 contact->img_avatar=ImageRef(new Image(imageFile.c_str()));
	 img.reset();
	 img=ImageRef(new Image(imageFile.c_str()));
	Log::getInstance()->msg("[***SUCCESS.]("+contact->statusMessage+")");
	//привязка к контакту


	/*
	  void Log::msg(const std::string &message){
	    addLog(utf8::utf8_wchar(message).c_str());
	  }
	  void Log::addLog(const wchar_t * msg) {}
	*/
	delete dst;
    return TRUE;
}