bool UT_ByteBuf::insertFromURI(UT_uint32 iPosition, const char *szURI) { GsfInput *fp = UT_go_file_open(szURI, NULL); if(!fp) return false; bool res = insertFromInput (iPosition, fp); g_object_unref (fp); return res; }
UT_Error IE_Imp::importFile (const char * szFilename) { GsfInput * input; input = UT_go_file_open (szFilename, NULL); if (!input) return UT_IE_FILENOTFOUND; UT_Error result = _loadFile (input); g_object_unref (G_OBJECT (input)); return result; }
UT_Error IE_Imp::loadFile(PD_Document * doc, const char * szFilename, IEFileType ieft, const char * props, IEFileType * savedAsType) { GsfInput * input; input = UT_go_file_open (szFilename, NULL); if (!input) return UT_IE_FILENOTFOUND; UT_Error result = loadFile (doc, input, ieft, props, savedAsType); g_object_unref (G_OBJECT (input)); return result; }
/*! Construct an importer of the right type. \param pDocument Document \param szFilename Name of file - optional \param ieft Desired filetype - pass IEFT_Unknown for best guess \param ppie Pointer to return importer in \param pieft Pointer to fill in actual filetype Caller is responsible for deleting the importer object when finished with it. This function should closely match IE_Exp::contructExporter() */ UT_Error IE_Imp::constructImporter(PD_Document * pDocument, const char * szFilename, IEFileType ieft, IE_Imp ** ppie, IEFileType * pieft) { GsfInput * input = NULL; if (szFilename) input = UT_go_file_open (szFilename, NULL); if (input || (szFilename == NULL)) { UT_Error result = constructImporter(pDocument, input, ieft, ppie, pieft); if (input) g_object_unref (G_OBJECT (input)); return result; } return UT_IE_FILENOTFOUND; }
UT_Error IE_Exp_EPUB::compress() { GsfInfile* oebpsDir = gsf_infile_stdio_new( UT_go_filename_from_uri(m_oebpsDir.c_str()), NULL); if (oebpsDir == NULL) { UT_DEBUGMSG(("RUDYJ: Can`t open temporary OEBPS directory\n")); return UT_ERROR; } std::vector<std::string> listing = getFileList( UT_go_filename_from_uri(m_oebpsDir.c_str())); for (std::vector<std::string>::iterator i = listing.begin(); i != listing.end(); i++) { GsfOutput* item = gsf_outfile_new_child(GSF_OUTFILE(m_oebps), (*i).c_str(), FALSE); std::string fullPath = m_oebpsDir + G_DIR_SEPARATOR_S + *i; GsfInput* file = UT_go_file_open(fullPath.c_str(), NULL); if (file == NULL) { UT_DEBUGMSG(("RUDYJ: Can`t open file\n")); return UT_ERROR; } gsf_output_seek(item, 0, G_SEEK_SET); gsf_input_seek(file, 0, G_SEEK_SET); gsf_input_copy(file, item); gsf_output_close(item); // Time to delete temporary file UT_go_file_remove(fullPath.c_str(), NULL); } UT_go_file_remove((m_oebpsDir + G_DIR_SEPARATOR_S + "index.xhtml_files").c_str(), NULL); UT_go_file_remove(m_oebpsDir.c_str(), NULL); return UT_OK; }
UT_Error IE_Imp_EPUB::readStructure() { getDoc()->createRawDocument(); getDoc()->finishRawCreation(); for (std::vector<std::string>::iterator i = m_spine.begin(); i != m_spine.end(); i++) { std::map<std::string, std::string>::iterator iter = m_manifestItems.find(*i); if (iter == m_manifestItems.end()) { UT_DEBUGMSG(("Manifest item with id %s not found\n", (*i).c_str())); return UT_ERROR; } std::string itemPath = m_tmpDir + G_DIR_SEPARATOR_S + (iter->second); PT_DocPosition posEnd = 0; getDoc()->getBounds(true, posEnd); if (i != m_spine.begin()) { getDoc()->insertStrux(posEnd, PTX_Section, NULL, NULL); getDoc()->insertStrux(posEnd+1, PTX_Block, NULL, NULL); posEnd+=2; } GsfInput* itemInput = UT_go_file_open(itemPath.c_str(), NULL); if (itemInput == NULL) { UT_DEBUGMSG(("Can`t open item for reading\n")); return UT_ERROR; } PD_Document *currentDoc = new PD_Document(); currentDoc->createRawDocument(); const char *suffix = strchr(itemPath.c_str(), '.'); XAP_App::getApp()->getPrefs()->setIgnoreNextRecent(); if (currentDoc->importFile(itemPath.c_str(), IE_Imp::fileTypeForSuffix(suffix), true, false, NULL) != UT_OK) { UT_DEBUGMSG(("Failed to import file %s\n", itemPath.c_str())); return UT_ERROR; } currentDoc->finishRawCreation(); // const gchar * attributes[3] = { // "listid", // "0", // 0 // }; // PT_DocPosition pos; // currentDoc->getBounds(true, pos); // currentDoc->insertStrux(pos, PTX_Block, attributes, NULL, NULL); IE_Imp_PasteListener * pPasteListener = new IE_Imp_PasteListener( getDoc(), posEnd, currentDoc); currentDoc->tellListener(static_cast<PL_Listener *> (pPasteListener)); DELETEP(pPasteListener); UNREFP(currentDoc); g_object_unref(G_OBJECT(itemInput)); } return UT_OK; }
gint XAP_UnixDialog_FileOpenSaveAs::previewPicture (void) { UT_ASSERT (m_FC && m_preview); UT_ASSERT(XAP_App::getApp()); const XAP_StringSet * pSS = m_pApp->getStringSet(); UT_return_val_if_fail( pSS, 0 ); // attach and clear the area immediately GR_UnixCairoAllocInfo ai(m_preview); GR_CairoGraphics* pGr = (GR_CairoGraphics*) XAP_App::getApp()->newGraphics(ai); const gchar * file_name = gtk_file_chooser_get_uri (m_FC); GR_Font * fnt = pGr->findFont("Times New Roman", "normal", "", "normal", "", "12pt", pSS->getLanguageName()); pGr->setFont(fnt); UT_UTF8String str; pSS->getValueUTF8(XAP_STRING_ID_DLG_IP_No_Picture_Label, str); int answer = 0; FG_Graphic * pGraphic = 0; GR_Image *pImage = NULL; double scale_factor = 0.0; UT_sint32 scaled_width,scaled_height; UT_sint32 iImageWidth,iImageHeight; { GR_Painter painter(pGr); painter.clearArea(0, 0, pGr->tlu(m_preview->allocation.width), pGr->tlu(m_preview->allocation.height)); if (!file_name) { painter.drawChars (str.ucs4_str().ucs4_str(), 0, str.size(), pGr->tlu(12), pGr->tlu(static_cast<int>(m_preview->allocation.height / 2)) - pGr->getFontHeight(fnt)/2); goto Cleanup; } // are we dealing with a file or directory here? struct stat st; if (!stat (file_name, &st)) { if (!S_ISREG(st.st_mode)) { painter.drawChars (str.ucs4_str().ucs4_str(), 0, str.size(), pGr->tlu(12), pGr->tlu(static_cast<int>(m_preview->allocation.height / 2)) - pGr->getFontHeight(fnt)/2); goto Cleanup; } } GsfInput * input = NULL; UT_DEBUGMSG(("file_name %s \n",file_name)); input = UT_go_file_open (file_name, NULL); if (!input) goto Cleanup; char Buf[4097] = ""; // 4096+nul ought to be enough UT_uint32 iNumbytes = UT_MIN(4096, gsf_input_size(input)); gsf_input_read(input, iNumbytes, (guint8 *)(Buf)); Buf[iNumbytes] = '\0'; IEGraphicFileType ief = IE_ImpGraphic::fileTypeForContents(Buf,4096); if((ief == IEGFT_Unknown) || (ief == IEGFT_Bogus)) { painter.drawChars (str.ucs4_str().ucs4_str(), 0, str.size(), pGr->tlu(12), pGr->tlu(static_cast<int>(m_preview->allocation.height / 2)) - pGr->getFontHeight(fnt)/2); g_object_unref (G_OBJECT (input)); goto Cleanup; } g_object_unref (G_OBJECT (input)); input = UT_go_file_open (file_name, NULL); size_t num_bytes = gsf_input_size(input); UT_Byte * bytes = (UT_Byte *) gsf_input_read(input, num_bytes,NULL ); if(bytes == NULL) { painter.drawChars (str.ucs4_str().ucs4_str(), 0, str.size(), pGr->tlu(12), pGr->tlu(static_cast<int>(m_preview->allocation.height / 2)) - pGr->getFontHeight(fnt)/2); g_object_unref (G_OBJECT (input)); goto Cleanup; } UT_ByteBuf * pBB = new UT_ByteBuf(); pBB->append(bytes,num_bytes); g_object_unref (G_OBJECT (input)); // // OK load the data into a GdkPixbuf // bool bLoadFailed = false; // GdkPixbuf * pixbuf = pixbufForByteBuf ( pBB); delete pBB; if(pixbuf == NULL) { // // Try a fallback loader here. // painter.drawChars (str.ucs4_str().ucs4_str(), 0, str.size(), pGr->tlu(12), pGr->tlu(static_cast<int>(m_preview->allocation.height / 2)) - pGr->getFontHeight(fnt)/2); bLoadFailed = true; goto Cleanup; } pImage = new GR_UnixImage(NULL,pixbuf); iImageWidth = gdk_pixbuf_get_width (pixbuf); iImageHeight = gdk_pixbuf_get_height (pixbuf); if (m_preview->allocation.width >= iImageWidth && m_preview->allocation.height >= iImageHeight) scale_factor = 1.0; else scale_factor = MIN( static_cast<double>(m_preview->allocation.width)/iImageWidth, static_cast<double>(m_preview->allocation.height)/iImageHeight); scaled_width = static_cast<int>(scale_factor * iImageWidth); scaled_height = static_cast<int>(scale_factor * iImageHeight); static_cast<GR_UnixImage *>(pImage)->scale(scaled_width,scaled_height); painter.drawImage(pImage, pGr->tlu(static_cast<int>((m_preview->allocation.width - scaled_width ) / 2)), pGr->tlu(static_cast<int>((m_preview->allocation.height - scaled_height) / 2))); answer = 1; } Cleanup: FREEP(file_name); DELETEP(pImage); DELETEP(pGr); DELETEP(pGraphic); return answer; }
void AbiCollabSessionManager::loadProfile() { UT_DEBUGMSG(("AbiCollabSessionManager::loadProfile()\n")); gchar *s = g_build_filename(XAP_App::getApp()->getUserPrivateDirectory(), "AbiCollab.Profile", (void*)0); UT_UTF8String profile(s); FREEP(s); GsfInput* in = NULL; char *uri = UT_go_filename_to_uri(profile.utf8_str()); UT_return_if_fail(uri); in = UT_go_file_open(uri, NULL); // TODO: shouldn't use NULL here, but check for errors FREEP(uri); if (!in) return; guint8 const* contents = gsf_input_read(in, gsf_input_size(in), NULL); if (contents) { xmlDocPtr reader = xmlReadMemory(reinterpret_cast<const char*>(contents), strlen(reinterpret_cast<const char*>(contents)), 0, "UTF-8", 0); if (reader) { xmlNode* node = xmlDocGetRootElement(reader); if (node) { if (strcmp(reinterpret_cast<const char*>(node->name), "AbiCollabProfile") == 0) { for (xmlNode* accountNode = node->children; accountNode; accountNode = accountNode->next) { // TODO: check if this node is really an AccountHandler node // find the account handler belonging to this type xmlChar* prop = xmlGetProp(accountNode, BAD_CAST "type"); UT_UTF8String handlerType = reinterpret_cast<char *>(prop); xmlFree(prop); std::map<UT_UTF8String, AccountHandlerConstructor>::iterator handler_iter = m_regAccountHandlers.find(handlerType); if (handler_iter == m_regAccountHandlers.end()) continue; // could happen for example when the sugar backend is found in the profile, which does not have a registered account handler belowing to it for now AccountHandlerConstructor constructor = handler_iter->second; AccountHandler* pHandler = constructor(); UT_continue_if_fail(pHandler); for (xmlNode* accountProp = accountNode->children; accountProp; accountProp = accountProp->next) { if (accountProp->type == XML_ELEMENT_NODE) { // some node names are pre-defined... if (strcmp(reinterpret_cast<const char*>(accountProp->name), "buddies") == 0) { for (xmlNode* buddyNode = accountProp->children; buddyNode; buddyNode = buddyNode->next) { if (buddyNode->type != XML_ELEMENT_NODE) continue; UT_continue_if_fail(strcmp(reinterpret_cast<const char*>(buddyNode->name), "buddy") == 0); UT_continue_if_fail(buddyNode->children); // read all buddy properties PropertyMap vBuddyProps; for (xmlNode* buddyPropertyNode = buddyNode->children; buddyPropertyNode; buddyPropertyNode = buddyPropertyNode->next) { UT_continue_if_fail(buddyPropertyNode->type == XML_ELEMENT_NODE); UT_UTF8String buddyPropValue = reinterpret_cast<const char*>(xmlNodeGetContent(buddyPropertyNode)); UT_continue_if_fail(buddyPropertyNode->name && *buddyPropertyNode->name && buddyPropValue.size() > 0); vBuddyProps.insert(PropertyMap::value_type( reinterpret_cast<const char*>(buddyPropertyNode->name), buddyPropValue.utf8_str()) ); } // construct the buddy BuddyPtr pBuddy = pHandler->constructBuddy(vBuddyProps); if (pBuddy) { // add the buddy to the account handler pHandler->addBuddy(pBuddy); } } } else { // ... the rest are generic properties UT_UTF8String propValue = reinterpret_cast<const char*>(xmlNodeGetContent(accountProp)); pHandler->addProperty(reinterpret_cast<const char*>(accountProp->name), propValue.utf8_str()); } } } // add the account to the account list if it is not a duplicate if (addAccount(pHandler)) { if (pHandler->autoConnect()) pHandler->connect(); } else { _deleteAccount(pHandler); } } } } xmlFreeDoc(reader); } } g_object_unref(G_OBJECT(in)); }