/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 * 
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is Komodo code.
 * 
 * The Initial Developer of the Original Code is ActiveState Software Inc.
 * Portions created by ActiveState Software Inc are Copyright (C) 2000-2007
 * ActiveState Software Inc. All Rights Reserved.
 * 
 * Contributor(s):
 *   ActiveState Software Inc
 * 
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK ***** */

#include "nsISupports.idl"
#include "nsITreeView.idl"

interface ISciMoz;
interface koIFindSession;
interface koIFindResultsTabManager;
interface koIFindResultsView;
interface koIFindContext;

/**
 * JS-implemented controller for driving the "Confirm Replacements"
 * dialog.
 */
[scriptable, uuid(9cd5dae7-07f5-49e2-8b7b-d18a6f7a4c53)]
interface koIConfirmReplaceController : nsISupports {
    // Called by koIConfirmReplacerInFiles to indicate that all
    // replacements have been found.
    void done();

    // Called to report the current status of the search/replace.
    void report(in PRUint32 num_hits,
                in PRUint32 num_paths_with_hits,
                in PRUint32 num_paths_searched,
                in PRUint32 num_paths_skipped);
};


/**
 * Component that handles a single "replace all in files" session with
 * confirmation. Typical usage is:
 *
 *   replacer = koIFindService.confirmreplaceallinfiles(...);
 *   replacer.start();
 */
[scriptable, uuid(9ad2b49f-b71f-4c32-b2f5-478e5adbc46a)]
interface koIConfirmReplacerInFiles : nsITreeView {
    readonly attribute koIConfirmReplaceController controller;

    readonly attribute PRUint32 num_hits;
    readonly attribute PRUint32 num_paths_with_hits;
    readonly attribute PRUint32 num_paths_searched;
    readonly attribute wstring journal_id;  // id to use for 'undo'

    void start();
    boolean isAlive();
    void stop();

    void toggle_mark(in PRUint32 row_idx);
    AString diff_from_indeces(
        in PRUint32 count,
        [array, size_is(count)] in PRUint32 indeces);
    AString marked_diff();

    /**
     * Make the marked replacements and post results to the given
     * results manager UI.
     *
     * @exception ServerException Raises an error and sets a "last error"
     *      if the replacer thread is still running. I.e. the controller
     *      must wait until `controller.done()` is called before calling
     *      commit.
     */
    void commit(in koIFindResultsTabManager resultsMgr);
};


/**
 * JS-implemented controller for driving the "Undo Replacements"
 * dialog. This is generally driven by a `koIReplaceUndoer` instance.
 */
[scriptable, uuid(a47a52dc-f93e-4f0f-ad5e-0ecff91916cc)]
interface koIUndoReplaceController : nsISupports {
    void set_summary(in AString summary);

    /**
     * Called to report the undo status so far.
     */
    void report(in PRUint32 num_hits, in PRUint32 num_paths);

    /**
     * Called to indicate that the undo was successfully completed.
     */
    void done();
    
    /**
     * Called to indicate that the undo failed.
     *
     * @param errmsg {string} A failure message.
     */
    void error(in AString errmsg);
};


/**
 * Component that handles undoing a "replace all in files".
 * Typical usage is:
 *
 *   undoer = koIFindService.undoreplaceallinfiles(...);
 *   undoer.start();
 */
[scriptable, uuid(44e52fe4-a8ee-4877-bc21-61a793f063b3)]
interface koIReplaceUndoer : nsITreeView {
    readonly attribute koIUndoReplaceController controller;

    void start();
    boolean isAlive();
    void stop();
};


[scriptable, uuid(94366D2F-6C10-455b-90B7-EC077161CDA2)]
interface koIFindResult : nsISupports {
    // The id for a searchable view.
    //XXX Should change the name to viewId (or similar) at some point.
    attribute wstring url;
    // zero-based indeces into the search text demarking the find result
    attribute PRUint32 start;
    attribute PRUint32 end;
    // the string found (not sure if this is really necessary, but should
    // be useful for debugging)
    readonly attribute AString value;

    // Some data that can be filled in lazily. This data is not guaranteed to
    // be set.
    attribute PRUint32 line;
    attribute PRUint32 column;
    attribute AString context_;
};


[scriptable, uuid(2EE0CACF-D3BF-44a8-BCAC-952D6418B1C6)]
interface koIReplaceResult : koIFindResult {
    // the string with which to replace the value
    readonly attribute AString replacement;
};


[scriptable, uuid(BB453BC9-FCF7-4625-A3FB-590418F48BF5)]
interface koIFindOptions: nsISupports {
    // pattern types
    attribute PRUint32 patternType;
    const long FOT_SIMPLE = 0;       // search for a simple string
    const long FOT_WILDCARD = 1;     // use shell-like filename matching of the search pattern
    const long FOT_REGEX_PYTHON = 2; // the search pattern is a regex (Python syntax)

    attribute boolean matchWord;    // whether to match whole words
    attribute boolean searchBackward;

    // case sensitivity options
    attribute PRUint32 caseSensitivity;
    const long FOC_INSENSITIVE = 0;  // case insensitive searching
    const long FOC_SENSITIVE = 1;    // case sensitive
    const long FOC_SMART = 2;        // case sensitive iff pattern includes any uppercase letters

    // One of FCT_CURRENT_DOC or FCT_ALL_OPEN_DOCS.
    // This is set by the find dialog (when the user manually switches the
    // context there) and is used by cmd_findNext and cmd_findPrevious in
    // the main Komodo window. Opening the find dialog resets this to
    // FCT_CURRENT_DOC (see bug 20003 for why).
    //
    // Persisted in the 'find-preferredContextType' pref.
    attribute long preferredContextType; 

    attribute boolean showReplaceAllResults;
    attribute boolean displayInFindResults2;

    // Fallback cwd for interpreting relative dirs for searching.
    attribute wstring cwd;

    // Find in Files stuff:
    // - The folders in which to search
    //   This is a _list_ of folders but this list is often represented in
    //   the UI as a string encoded by joining the folders with the
    //   native path separator. E.g. on Windows: "D:\trentm;D:\tmp". It is
    //   easier with XPCOM to just pass around the encoded string.
    attribute wstring encodedFolders;
    void getFolders(out PRUint32 count,
                    [retval, array, size_is(count)] out wstring folders);
    // - Whether to search in subfolders
    attribute boolean searchInSubfolders;
    // - The filetype to search (see 'encoding' note with folders)
    attribute wstring encodedIncludeFiletypes;
    void getIncludeFiletypes(out PRUint32 count,
                             [retval, array, size_is(count)] out wstring filetypes);
    attribute wstring encodedExcludeFiletypes;
    void getExcludeFiletypes(out PRUint32 count,
                             [retval, array, size_is(count)] out wstring filetypes);

    // Other stuff:
    attribute boolean multiline;
    attribute boolean confirmReplacementsInFiles;
    
    // Return a string description of the search. E.g., if this
    // is a regex search then the result might be 'foo' -> '/foo/i'
    AString searchDescFromPattern(in AString pattern);
};


[scriptable, uuid(19A373A9-77EC-4c96-B86F-1928018AB924)]
interface koIFindService : nsISupports {
    // Error notes:
    //  If an exception is raise in any of the Find service's methods
    //  then details of the error are set on the koILastErrorService.
    //  Only an error *message* is set, not an error number.

    // Return the result of searching for the first "pattern" in "text".
    koIFindResult find(in wstring url,
                       in AString text,
                       in AString pattern,
                       in long start,
                       in long end);

    // Return a result indicating how to replace the first "pattern" in
    // "text" with "replacement".
    koIReplaceResult replace(in wstring url,
                             in AString text,
                             in AString pattern,
                             in AString replacement,
                             in long startOffset,
                             in ISciMoz scimoz);

    // A faster alternative to findall() specific to Komodo usage.
    // See the implementation for more docs.
    void findallex(in wstring url,
                   in AString text,
                   in AString pattern,
                   in koIFindResultsView resultsView,
                   in PRUint32 contextOffset,
                   in ISciMoz scimoz);

    // Find all lines on which the given pattern is found.
    // Returns a list of lines (0-based).
    void findalllines(in wstring url,
                      in AString text,
                      in AString pattern,
                      in PRUint32 contextOffset,
                      in ISciMoz scimoz,
                      out PRUint32 count,
                      [retval, array, size_is(count)] out PRUint32 lines);
    
    /**
     * Hightlight all matches in the scintilla buffer.
     *
     * @param scimoz   - a Scintilla instance
     * @param pattern  - the search pattern to highlight
     * @param start    - the starting text position (character position)
     * @param end      - the ending text position (character position). If the
     *                   end is the same as the start, then the whole of the
     *                   scimoz text will be searched.
     * @param timeout  - stop highlighting after this number of milliseconds
     *                   have passed by.
     */
    void highlightall(in ISciMoz scimoz,
                      in AString pattern,
                      in long start,
                      in long end,
                      in long timeout);

    /**
     * Hightlight all the last find/replace matches in the scintilla buffer.
     *
     * @param scimoz   - a Scintilla instance
     */
    void highlightlastresults(in ISciMoz scimoz);

    // A faster alternative to replaceall() specific to Komodo usage.
    // See the implementation for more docs.
    AString replaceallex(in wstring url,
                         in AString text,
                         in AString pattern,
                         in AString replacement,
                         in boolean firstOnLine,
                         in koIFindSession session,
                         in koIFindResultsView resultsView,
                         in PRUint32 contextOffset,
                         in ISciMoz scimoz,
                         out PRUint32 count); // number of replacements

    // Asynchronously find all occurrences of "pattern" and load those into
    // the "resultsView". Note: the information on which files to search are
    // in the "options" attribute.
    //XXX Need to add a .finalize() to the find service to .stop() and
    //    .join() any findallinfiles thread.
    void findallinfiles(
            in long id, // session id
            in AString pattern,
            in koIFindResultsTabManager resultsMgr);

    // Asynchronously replace all "pattern" with "repl" and load those
    // into the "resultsView". Note: the information on which files to
    // search are either in `resultsMgr.context_` (if a
    // koICollectionFindContext) or in the "options" attribute.
    //XXX Need to add a .finalize() to the find service to .stop() and
    //    .join() any replaceallinfiles thread.
    void replaceallinfiles(
            in long id, // session id
            in AString pattern,
            in AString repl,
            in koIFindResultsTabManager resultsMgr);

    koIConfirmReplacerInFiles confirmreplaceallinfiles(
            in AString pattern,
            in AString repl,
            in koIFindContext context_,
            in koIConfirmReplaceController controller);
    
    koIReplaceUndoer undoreplaceallinfiles(
            in wstring journalId,
            in koIUndoReplaceController controller);

    // Abort a running findallinfiles, replaceallinfiles or
    // findallincollection session.
    void stopfindreplaceinfiles(in long id);

    AString regex_escape_string(in AString s);

    // persistent find options
    attribute koIFindOptions options;
};

