Exemplo n.º 1
0
GsfOutput* IE_Imp_EPUB::createFileByPath(const char* path)
{
    gchar** components = g_strsplit(path, G_DIR_SEPARATOR_S, 0);
    std::string curPath = "";

    int current = 0;
    GsfOutput* output = NULL;
    while (components[current] != NULL)
    {
        curPath += components[current];
        current++;

        char *uri = UT_go_filename_to_uri(curPath.c_str());
        bool fileExists = UT_go_file_exists(uri);
        if (!fileExists && (components[current] != NULL))
        {
            UT_go_directory_create(uri, 0644, NULL);
        }
        else
        {
            if (!fileExists)
            {
                output = UT_go_file_create(uri, NULL);
                break;
            }
        }

        g_free(uri);

        if (components[current] != NULL)
        {
            curPath += G_DIR_SEPARATOR_S;
        }
    }

    g_strfreev(components);
    return output;
}
bool XAP_UnixDialog_FileOpenSaveAs::_run_gtk_main(XAP_Frame * pFrame,
        GtkWidget * filetypes_pulldown)
{
    /*
      Run the dialog in a loop to catch bad filenames.
      The location of this check being in this dialog loop
      could be considered temporary.  Doing this matches the Windows
      common control behavior (where the dialog checks everything
      for the programmer), but lacks flexibility for different
      uses of this dialog (file export, print export, directory
      (not file) selection).

      This check might need to be moved into the ap code which calls
      this dialog, and certain interfaces exposed so that the
      dialog is displayed throughout the verification.

      For right now you can signal this check on and off with
      bCheckWritePermission.
    */

    char * szDialogFilename = NULL;		// this is the file name returned from the dialog
    char * szFinalPathname = NULL;		// this is the file name after suffix addition, if any
    char * szFinalPathnameCopy = NULL;	// one to mangle when looking for dirs, etc.

    char * pLastSlash;

    // if m_bSave is not set, we're looking to OPEN a file.
    // otherwise we are looking to SAVE a file.
    if (!m_bSave)
    {
        while (1)
        {
            gtk_main();
            if (m_answer == a_CANCEL)			// The easy way out
                return false;

            m_szFinalPathnameCandidate = gtk_file_chooser_get_uri(m_FC);
            UT_ASSERT(m_szFinalPathnameCandidate);
            return (m_answer == a_OK);
        }
    }
    else
    {
        while(1)
        {
            gtk_main();
            if (m_answer == a_CANCEL)			// The easy way out
                return false;

            // Give us a filename we can mangle

            szDialogFilename = gtk_file_chooser_get_uri(m_FC);
            if (!szDialogFilename)
                continue;

            // We append the suffix of the default type, so the user doesn't
            // have to.  This is adapted from the Windows front-end code
            // (xap_Win32Dlg_FileOpenSaveAs.cpp), since it should act the same.
            // If, however, the user doesn't want suffixes, they don't have to have them.
            {
                //UT_uint32 end = g_strv_length(m_szSuffixes);
                UT_sint32 nFileType = XAP_comboBoxGetActiveInt(GTK_COMBO_BOX(filetypes_pulldown));

                // set to first item, which should probably be auto detect
                // TODO : "probably" isn't very good.
                UT_uint32 nIndex = 0;

                // the index in the types table will match the index in the suffix
                // table.  nFileType is the data we are searching for.
                if(m_nTypeList != NULL)
                {
                    for (UT_uint32 i = 0; m_nTypeList[i]; i++)
                    {
                        if (m_nTypeList[i] == nFileType)
                        {
                            nIndex = i;
                            break;
                        }
                    }
                }

                bool wantSuffix = true;
                XAP_Prefs *pPrefs= XAP_App::getApp()->getPrefs();
                pPrefs->getPrefsValueBool(static_cast<const gchar *>(XAP_PREF_KEY_UseSuffix), &wantSuffix);
                UT_DEBUGMSG(("UseSuffix: %d\n", wantSuffix));

                if (nFileType > 0 && getDialogId() != XAP_DIALOG_ID_FILE_SAVE_IMAGE) // 0 means autodetect
                {
                    if (!UT_pathSuffix(szDialogFilename).empty())
                    {
                        // warn if we have a suffix that doesn't match the selected file type
                        IE_ExpSniffer* pSniffer = IE_Exp::snifferForFileType(m_nTypeList[nIndex]);
                        if (pSniffer && !pSniffer->recognizeSuffix(UT_pathSuffix(szDialogFilename).c_str()))
                        {
                            UT_UTF8String msg;
                            const XAP_StringSet * pSS = m_pApp->getStringSet();
                            pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_ExtensionDoesNotMatch, msg);
                            if (pFrame->showMessageBox(msg.utf8_str(), XAP_Dialog_MessageBox::b_YN, XAP_Dialog_MessageBox::a_NO) != XAP_Dialog_MessageBox::a_YES)
                                goto ContinueLoop;
                        }
                        szFinalPathname = g_strdup(szDialogFilename);
                    }
                    else if (wantSuffix)
                    {
                        // if the file doesn't have a suffix already, and the file type
                        // is normal (special types are negative, like auto detect),
                        // and the user wants extensions, slap a suffix on it.
                        // add suffix based on selected file type
                        UT_UTF8String suffix (IE_Exp::preferredSuffixForFileType(m_nTypeList[nIndex]));
                        UT_uint32 length = strlen(szDialogFilename) + suffix.size() + 1;

                        szFinalPathname = static_cast<char *>(UT_calloc(length,sizeof(char)));

                        if (szFinalPathname)
                        {
                            char * p = szFinalPathname;
                            strcpy(p,szDialogFilename);
                            strcat(p,suffix.utf8_str());
                        }
                    }
                    else
                        szFinalPathname = g_strdup(szDialogFilename);
                }
                else
                {
                    // the file type is special (auto detect)
                    // set to plain name, and let the auto detector in the
                    // exporter figure it out
                    szFinalPathname = g_strdup(szDialogFilename);
                }

                // g_free szDialogFilename since it's been put into szFinalPathname (with
                // or without changes) and it's invalid (missing an extension which
                // might have been appended)

                FREEP(szDialogFilename);
            }

            szFinalPathnameCopy = g_strdup(szFinalPathname);

            if (UT_go_file_exists(szFinalPathnameCopy))
            {
                // we have an existing file, ask to overwrite
                if (_askOverwrite_YesNo(pFrame, szFinalPathname))
                {
                    m_szFinalPathnameCandidate = g_strdup(szFinalPathname);
                    goto ReturnTrue;
                }

                goto ContinueLoop;
            }

            // We have a string that may contain a path, and may have a file
            // at the end.  First, strip off a file (if it exists), and test
            // for a matching directory.  We can then proceed with the file
            // if another stat of that dir passes.

            if (szFinalPathnameCopy && strlen(szFinalPathnameCopy))
                pLastSlash = strrchr(szFinalPathnameCopy,'/');
            else
                pLastSlash = NULL;

            if (!pLastSlash)
            {
                _notifyError_OKOnly(pFrame,XAP_STRING_ID_DLG_InvalidPathname);
                goto ContinueLoop;
            }

            m_szFinalPathnameCandidate = g_strdup(szFinalPathname);
            goto ReturnTrue;

            // complain about write permission on the directory.
            // lop off ugly trailing slash only if we don't have
            // the root dir ('/') for a path

            if (pLastSlash > szFinalPathnameCopy)
                *pLastSlash = 0;

            _notifyError_OKOnly(pFrame,XAP_STRING_ID_DLG_NoSaveFile_DirNotWriteable,
                                szFinalPathname);
ContinueLoop:
            FREEP(szFinalPathnameCopy);
        }
    } /* if m_bSave */

    /*NOTREACHED*/

ReturnTrue:
    FREEP(szFinalPathnameCopy);
    FREEP(szFinalPathname);
    return true;
}