Esempio n. 1
0
void Copyright::OnInsertCopyrights(wxCommandEvent& e)
{
    wxUnusedVar(e);

    // read configuration
    CopyrightsConfigData data;
    m_mgr->GetConfigTool()->ReadObject(wxT("CopyrightsConfig"), &data);

    // make sure that the template file exists
    if(!wxFileName::FileExists(data.GetTemplateFilename())) {
        wxMessageBox(
            wxString::Format(_("Template file name '%s', does not exist!"), data.GetTemplateFilename().GetData()),
            _("CodeLite"), wxICON_WARNING | wxOK);
        return;
    }

    // read the copyrights file
    wxString content;
    if(!ReadFileWithConversion(data.GetTemplateFilename(), content)) {
        wxMessageBox(wxString::Format(_("Failed to read template file '%s'"), data.GetTemplateFilename().c_str()),
                     _("CodeLite"), wxICON_WARNING | wxOK);
        return;
    }

    IEditor* editor = m_mgr->GetActiveEditor();
    if(!editor) {
        wxMessageBox(wxString::Format(_("There is no active editor\n")), _("CodeLite"), wxICON_WARNING | wxOK);
        return;
    }

    // verify that the file consist only with comment code
    CppWordScanner scanner(data.GetTemplateFilename().mb_str().data());

    CppTokensMap l;
    scanner.FindAll(l);

    if(!l.is_empty()) {
        if(wxMessageBox(_("Template file contains text which is not comment, continue anyway?"), _("CodeLite"),
                        wxICON_QUESTION | wxYES_NO) == wxNO) {
            return;
        }
    }

    // expand constants
    wxString _content = ExpandAllVariables(content, m_mgr->GetWorkspace(), wxEmptyString, wxEmptyString,
                                           editor->GetFileName().GetFullPath());

    // we are good to go :)
    wxString ignoreString = data.GetIgnoreString();
    ignoreString = ignoreString.Trim().Trim(false);

    if(ignoreString.IsEmpty() == false) {
        if(editor->GetEditorText().Find(data.GetIgnoreString()) != wxNOT_FOUND) {
            clLogMessage(_("File contains ignore string, skipping it"));
            return;
        }
    }
    editor->InsertText(0, _content);
}
Esempio n. 2
0
void Copyright::MassUpdate(const std::vector<wxFileName>& filtered_files, const wxString& content)
{
    // last confirmation from the user
    if(wxMessageBox(
           wxString::Format(_("You are about to modify %u files. Continue?"), (unsigned int)filtered_files.size()),
           _("CodeLite"),
           wxYES_NO | wxICON_QUESTION) == wxNO) {
        return;
    }

    clProgressDlg* prgDlg = NULL;
    prgDlg = new clProgressDlg(NULL, _("Processing file ..."), wxT(""), (int)filtered_files.size());

    CopyrightsConfigData data;
    m_mgr->GetConfigTool()->ReadObject(wxT("CopyrightsConfig"), &data);

    // now loop over the files and add copyrights block
    for(size_t i = 0; i < filtered_files.size(); i++) {
        wxFileName fn = filtered_files.at(i);

        wxString file_content;
        wxString _content =
            ExpandAllVariables(content, m_mgr->GetWorkspace(), wxEmptyString, wxEmptyString, fn.GetFullPath());
        if(ReadFileWithConversion(fn.GetFullPath(), file_content)) {

            wxString msg;

            // if the file contains the ignore string, skip this file
            wxString ignoreString = data.GetIgnoreString();
            ignoreString = ignoreString.Trim().Trim(false);

            if(ignoreString.IsEmpty() == false && file_content.Find(data.GetIgnoreString()) != wxNOT_FOUND) {
                msg << _("File contains ignore string, skipping it: ") << fn.GetFullName();
                if(!prgDlg->Update(i, msg)) {
                    prgDlg->Destroy();
                    return;
                }
            } else {

                msg << _("Inserting comment to file: ") << fn.GetFullName();
                if(!prgDlg->Update(i, msg)) {
                    prgDlg->Destroy();
                    return;
                }

                file_content.Prepend(_content);
                WriteFileWithBackup(fn.GetFullPath(), file_content, data.GetBackupFiles());
            }
        }
    }
    prgDlg->Destroy();
}